priv_vfs_chroot.c revision 162271
1162271Srwatson/*-
2162271Srwatson * Copyright (c) 2006 nCircle Network Security, Inc.
3162271Srwatson * All rights reserved.
4162271Srwatson *
5162271Srwatson * This software was developed by Robert N. M. Watson for the TrustedBSD
6162271Srwatson * Project under contract to nCircle Network Security, Inc.
7162271Srwatson *
8162271Srwatson * Redistribution and use in source and binary forms, with or without
9162271Srwatson * modification, are permitted provided that the following conditions
10162271Srwatson * are met:
11162271Srwatson * 1. Redistributions of source code must retain the above copyright
12162271Srwatson *    notice, this list of conditions and the following disclaimer.
13162271Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14162271Srwatson *    notice, this list of conditions and the following disclaimer in the
15162271Srwatson *    documentation and/or other materials provided with the distribution.
16162271Srwatson *
17162271Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18162271Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19162271Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20162271Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
21162271Srwatson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22162271Srwatson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23162271Srwatson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24162271Srwatson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25162271Srwatson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26162271Srwatson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27162271Srwatson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28162271Srwatson *
29162271Srwatson * $FreeBSD: head/tools/regression/priv/priv_vfs_chroot.c 162271 2006-09-13 09:05:39Z rwatson $
30162271Srwatson */
31162271Srwatson
32162271Srwatson/*
33162271Srwatson * Test that chroot() requires privilege; try with, and without.  Do a no-op
34162271Srwatson * chroot() to "/".
35162271Srwatson *
36162271Srwatson * XXXRW: Would also be good to check fchroot() permission, but that is not
37162271Srwatson * exposed via the BSD API.
38162271Srwatson */
39162271Srwatson
40162271Srwatson#include <err.h>
41162271Srwatson#include <errno.h>
42162271Srwatson#include <unistd.h>
43162271Srwatson
44162271Srwatson#include "main.h"
45162271Srwatson
46162271Srwatsonvoid
47162271Srwatsonpriv_vfs_chroot(void)
48162271Srwatson{
49162271Srwatson	int error;
50162271Srwatson
51162271Srwatson	assert_root();
52162271Srwatson
53162271Srwatson	if (chroot("/") < 0)
54162271Srwatson		err(-1, "chroot(\"/\") as root");
55162271Srwatson
56162271Srwatson	set_euid(UID_OTHER);
57162271Srwatson
58162271Srwatson	error = chroot("/");
59162271Srwatson	if (error == 0)
60162271Srwatson		errx(-1, "chroot(\"/\") succeeded as !root");
61162271Srwatson	if (errno != EPERM)
62162271Srwatson		err(-1, "chroot(\"/\") wrong errno %d as !root", errno);
63162271Srwatson}
64