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 that privilege is required to set the sgid bit on a file with a group
35162271Srwatson * that isn't in the process credential.  The file uid owner is set to the
36162271Srwatson * uid being tested with, as we are not interested in testing privileges
37162271Srwatson * associated with file ownership.
38162271Srwatson */
39162271Srwatson
40162271Srwatson#include <sys/stat.h>
41162271Srwatson
42162271Srwatson#include <err.h>
43162271Srwatson#include <errno.h>
44162271Srwatson#include <fcntl.h>
45162271Srwatson#include <unistd.h>
46162271Srwatson
47162271Srwatson#include "main.h"
48162271Srwatson
49172106Srwatsonstatic char fpath[1024];
50172106Srwatsonstatic int fpath_initialized;
51162271Srwatson
52172106Srwatsonint
53172106Srwatsonpriv_vfs_setgid_fowner_setup(int asroot, int injail, struct test *test)
54162271Srwatson{
55162271Srwatson
56172106Srwatson	setup_file("priv_vfs_setgid_fowner: fpath", fpath, UID_OWNER,
57172106Srwatson	    GID_OWNER, 0600);
58172106Srwatson	fpath_initialized = 1;
59172106Srwatson	return (0);
60172106Srwatson}
61162271Srwatson
62172106Srwatsonint
63172106Srwatsonpriv_vfs_setgid_fother_setup(int asroot, int injail, struct test *test)
64172106Srwatson{
65162271Srwatson
66172106Srwatson	/* NOTE: owner uid, *other* gid. */
67172106Srwatson	setup_file("priv_vfs_setgid_forther: fpath", fpath, UID_OWNER,
68172106Srwatson	    GID_OTHER, 0600);
69172106Srwatson	fpath_initialized = 1;
70172106Srwatson	return (0);
71172106Srwatson}
72162271Srwatson
73172106Srwatsonvoid
74172106Srwatsonpriv_vfs_setgid_fowner(int asroot, int injail, struct test *test)
75172106Srwatson{
76172106Srwatson	int error;
77162271Srwatson
78172106Srwatson	error = chmod(fpath, 0600 | S_ISGID);
79172106Srwatson	if (asroot && injail)
80172106Srwatson		expect("priv_vfs_setgid_fowner(asroot, injail)", error, 0,
81172106Srwatson		    0);
82172106Srwatson	if (asroot && !injail)
83172106Srwatson		expect("priv_vfs_setgid_fowner(asroot, !injail)", error, 0,
84172106Srwatson		    0);
85172106Srwatson	if (!asroot && injail)
86172106Srwatson		expect("priv_vfs_setgid_fowner(!asroot, injail)", error, 0,
87172106Srwatson		    0);
88172106Srwatson	if (!asroot && !injail)
89172106Srwatson		expect("priv_vfs_setgid_fowner(!asroot, !injail)", error, 0,
90172106Srwatson		    0);
91172106Srwatson}
92162271Srwatson
93172106Srwatsonvoid
94172106Srwatsonpriv_vfs_setgid_fother(int asroot, int injail, struct test *test)
95172106Srwatson{
96172106Srwatson	int error;
97162271Srwatson
98172106Srwatson	error = chmod(fpath, 0600 | S_ISGID);
99172106Srwatson	if (asroot && injail)
100172106Srwatson		expect("priv_vfs_setgid_fother(asroot, injail)", error, 0,
101172106Srwatson		    0);
102172106Srwatson	if (asroot && !injail)
103172106Srwatson		expect("priv_vfs_setgid_fother(asroot, !injail)", error, 0,
104172106Srwatson		    0);
105172106Srwatson	if (!asroot && injail)
106172106Srwatson		expect("priv_vfs_setgid_fother(!asroot, injail)", error, -1,
107172106Srwatson		    EPERM);
108172106Srwatson	if (!asroot && !injail)
109172106Srwatson		expect("priv_vfs_setgid_fother(!asroot, !injail)", error, -1,
110172106Srwatson		    EPERM);
111172106Srwatson}
112162271Srwatson
113172106Srwatsonvoid
114172106Srwatsonpriv_vfs_setgid_cleanup(int asroot, int injail, struct test *test)
115172106Srwatson{
116162271Srwatson
117172106Srwatson	if (fpath_initialized) {
118172106Srwatson		(void)unlink(fpath);
119172106Srwatson		fpath_initialized = 0;
120162271Srwatson	}
121162271Srwatson}
122