1162271Srwatson/*-
2162271Srwatson * Copyright (c) 2006 nCircle Network Security, Inc.
3172106Srwatson * Copyright (c) 2007 Robert N. M. Watson
4162271Srwatson * All rights reserved.
5162271Srwatson *
6162271Srwatson * This software was developed by Robert N. M. Watson for the TrustedBSD
7162271Srwatson * Project under contract to nCircle Network Security, Inc.
8162271Srwatson *
9162271Srwatson * Redistribution and use in source and binary forms, with or without
10162271Srwatson * modification, are permitted provided that the following conditions
11162271Srwatson * are met:
12162271Srwatson * 1. Redistributions of source code must retain the above copyright
13162271Srwatson *    notice, this list of conditions and the following disclaimer.
14162271Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15162271Srwatson *    notice, this list of conditions and the following disclaimer in the
16162271Srwatson *    documentation and/or other materials provided with the distribution.
17162271Srwatson *
18162271Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19162271Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20162271Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21162271Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22162271Srwatson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23162271Srwatson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24162271Srwatson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25162271Srwatson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26162271Srwatson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27162271Srwatson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28162271Srwatson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29162271Srwatson *
30162271Srwatson * $FreeBSD$
31162271Srwatson */
32162271Srwatson
33162271Srwatson/*
34162271Srwatson * Test privilege check on /dev/io.  By default, the permissions also protect
35162271Srwatson * against non-superuser access, so this program will modify permissions on
36172106Srwatson * /dev/io to allow world access, and revert the change on exit.  This is not
37172106Srwatson * good for run-time security, but is necessary to test the checks properly.
38162271Srwatson */
39162271Srwatson
40162271Srwatson#include <sys/types.h>
41162271Srwatson#include <sys/stat.h>
42162271Srwatson
43162271Srwatson#include <err.h>
44162271Srwatson#include <errno.h>
45162271Srwatson#include <fcntl.h>
46162271Srwatson#include <unistd.h>
47162271Srwatson
48162271Srwatson#include "main.h"
49162271Srwatson
50172106Srwatson#define	NEW_PERMS	0666
51162271Srwatson#define	DEV_IO		"/dev/io"
52162271Srwatson#define	EXPECTED_PERMS	0600
53162271Srwatson
54172106Srwatsonstatic int initialized;
55172106Srwatsonstatic mode_t saved_perms;
56162271Srwatson
57172106Srwatsonint
58172106Srwatsonpriv_io_setup(int asroot, int asjail, struct test *test)
59162271Srwatson{
60162271Srwatson	struct stat sb;
61162271Srwatson
62172106Srwatson	if (stat(DEV_IO, &sb) < 0) {
63172106Srwatson		warn("priv_io_setup: stat(%s)", DEV_IO);
64172106Srwatson		return (-1);
65172106Srwatson	}
66162271Srwatson	saved_perms = sb.st_mode & ALLPERMS;
67172106Srwatson	if (saved_perms != EXPECTED_PERMS) {
68172106Srwatson		warnx("priv_io_setup: perms = 0%o; expected 0%o",
69172106Srwatson		    saved_perms, EXPECTED_PERMS);
70172106Srwatson		return (-1);
71172106Srwatson	}
72172106Srwatson	if (chmod(DEV_IO, NEW_PERMS) < 0) {
73172106Srwatson		warn("priv_io_setup: chmod(%s, 0%o)", DEV_IO, NEW_PERMS);
74172106Srwatson		return (-1);
75172106Srwatson	}
76172106Srwatson	initialized = 1;
77172106Srwatson	return (0);
78162271Srwatson}
79162271Srwatson
80172106Srwatsonvoid
81172106Srwatsonpriv_io(int asroot, int injail, struct test *test)
82162271Srwatson{
83172106Srwatson	int error, fd;
84162271Srwatson
85162271Srwatson	fd = open(DEV_IO, O_RDONLY);
86172106Srwatson	if (fd < 0)
87172106Srwatson		error = -1;
88172106Srwatson	else
89172106Srwatson		error = 0;
90172106Srwatson	if (asroot && injail)
91172106Srwatson		expect("priv_io(asroot, injail)", error, -1, EPERM);
92172106Srwatson	if (asroot && !injail)
93172106Srwatson		expect("priv_io(asroot, !injail)", error, 0, 0);
94172106Srwatson	if (!asroot && injail)
95172106Srwatson		expect("priv_io(!asroot, injail)", error, -1, EPERM);
96172106Srwatson	if (!asroot && !injail)
97172106Srwatson		expect("priv_io(!asroot, !injail)", error, -1, EPERM);
98172106Srwatson	if (fd != -1)
99162271Srwatson		close(fd);
100162271Srwatson}
101162271Srwatson
102162271Srwatsonvoid
103172106Srwatsonpriv_io_cleanup(int asroot, int asjail, struct test *test)
104162271Srwatson{
105162271Srwatson
106172106Srwatson	if (!initialized)
107172106Srwatson		return;
108172106Srwatson	if (chmod(DEV_IO, saved_perms) < 0)
109172106Srwatson		err(-1, "priv_io_cleanup: chmod(%s, 0%o)", DEV_IO,
110172106Srwatson		    saved_perms);
111172106Srwatson	initialized = 0;
112162271Srwatson}
113