1162271Srwatson/*- 2162271Srwatson * Copyright (c) 2006 nCircle Network Security, Inc. 3172106Srwatson * Copyright (c) 2007 Robert M. 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: releng/10.3/tools/regression/priv/priv_vfs_chown.c 172106 2007-09-09 23:08:39Z rwatson $ 31162271Srwatson */ 32162271Srwatson 33162271Srwatson/* 34162271Srwatson * Confirm that privilege is required in the cases using chown(): 35162271Srwatson * 36162271Srwatson * - If the process euid does not match the file uid. 37162271Srwatson * 38162271Srwatson * - If the target uid is different than the current uid. 39162271Srwatson * 40162271Srwatson * - If the target gid changes and we the process is not a member of the new 41162271Srwatson * group. 42162271Srwatson */ 43162271Srwatson 44162271Srwatson#include <sys/types.h> 45162271Srwatson#include <sys/stat.h> 46162271Srwatson 47162271Srwatson#include <err.h> 48162271Srwatson#include <errno.h> 49162271Srwatson#include <stdio.h> 50162271Srwatson#include <unistd.h> 51162271Srwatson 52162271Srwatson#include "main.h" 53162271Srwatson 54172106Srwatsonstatic char fpath[1024]; 55172106Srwatsonstatic int fpath_initialized; 56162271Srwatson 57172106Srwatson/* 58172106Srwatson * Check that changing the uid of a file requires privilege. 59172106Srwatson */ 60172106Srwatsonint 61172106Srwatsonpriv_vfs_chown_uid_setup(int asroot, int injail, struct test *test) 62172106Srwatson{ 63172106Srwatson 64172106Srwatson setup_file("priv_vfs_chown_uid: fpath", fpath, UID_ROOT, GID_WHEEL, 65172106Srwatson 0600); 66172106Srwatson fpath_initialized = 1; 67172106Srwatson return (0); 68172106Srwatson} 69172106Srwatson 70162271Srwatsonvoid 71172106Srwatsonpriv_vfs_chown_uid(int asroot, int injail, struct test *test) 72162271Srwatson{ 73162271Srwatson int error; 74162271Srwatson 75172106Srwatson error = chown(fpath, UID_OWNER, -1); 76172106Srwatson if (asroot && injail) 77172106Srwatson expect("priv_vfs_chown_uid(root, jail)", error, 0, 0); 78172106Srwatson if (asroot && !injail) 79172106Srwatson expect("priv_vfs_chown_uid(root, !jail)", error, 0, 0); 80172106Srwatson if (!asroot && injail) 81172106Srwatson expect("priv_vfs_chown_uid(!root, jail)", error, -1, EPERM); 82172106Srwatson if (!asroot && !injail) 83172106Srwatson expect("priv_vfs_chown_uid(!root, !jail)", error, -1, EPERM); 84172106Srwatson} 85162271Srwatson 86172106Srwatson/* 87172106Srwatson * Check that changing the gid of a file owned by the user is allowed without 88172106Srwatson * privilege as long as the gid matches the process. 89172106Srwatson */ 90172106Srwatsonint 91172106Srwatsonpriv_vfs_chown_mygid_setup(int asroot, int injail, struct test *test) 92172106Srwatson{ 93162271Srwatson 94162271Srwatson /* 95172106Srwatson * Create a file with a matching uid to the test process, but not a 96172106Srwatson * matching gid. 97162271Srwatson */ 98172106Srwatson setup_file("priv_vfs_chown_mygid: fpath", fpath, asroot ? UID_ROOT : 99172106Srwatson UID_OWNER, GID_OTHER, 0600); 100172106Srwatson fpath_initialized = 1; 101172106Srwatson return (0); 102172106Srwatson} 103162271Srwatson 104172106Srwatsonvoid 105172106Srwatsonpriv_vfs_chown_mygid(int asroot, int injail, struct test *test) 106172106Srwatson{ 107172106Srwatson int error; 108162271Srwatson 109172106Srwatson error = chown(fpath, -1, asroot ? GID_WHEEL : GID_OWNER); 110172106Srwatson if (asroot && injail) 111172106Srwatson expect("priv_vfs_chown_mygid(root, jail)", error, 0, 0); 112172106Srwatson if (asroot && !injail) 113172106Srwatson expect("priv_vfs_chown_mygid(root, !jail)", error, 0, 0); 114172106Srwatson if (!asroot && injail) 115172106Srwatson expect("priv_vfs_chown_mygid(!root, !jail)", error, 0, 0); 116172106Srwatson if (!asroot && !injail) 117172106Srwatson expect("priv_vfs_chown_mygid(!root, !jail)", error, 0, 0); 118172106Srwatson} 119162271Srwatson 120172106Srwatson/* 121172106Srwatson * Check that changing the gid of a file owned by the user is not allowed 122172106Srwatson * without privilege if the gid doesn't match the process. 123172106Srwatson */ 124172106Srwatsonint 125172106Srwatsonpriv_vfs_chown_othergid_setup(int asroot, int injail, struct test *test) 126172106Srwatson{ 127172106Srwatson 128162271Srwatson /* 129172106Srwatson * Create a file with a matching uid to the test process with a 130172106Srwatson * matching gid. 131162271Srwatson */ 132172106Srwatson setup_file("priv_vfs_chown_othergid: fpath", fpath, asroot ? UID_ROOT 133172106Srwatson : UID_OWNER, asroot ? GID_WHEEL : GID_OWNER, 0600); 134172106Srwatson fpath_initialized = 1; 135172106Srwatson return (0); 136172106Srwatson} 137162271Srwatson 138172106Srwatsonvoid 139172106Srwatsonpriv_vfs_chown_othergid(int asroot, int injail, struct test *test) 140172106Srwatson{ 141172106Srwatson int error; 142172106Srwatson 143162271Srwatson error = chown(fpath, -1, GID_OTHER); 144172106Srwatson if (asroot && injail) 145172106Srwatson expect("priv_vfs_chown_othergid(root, jail)", error, 0, 0); 146172106Srwatson if (asroot && !injail) 147172106Srwatson expect("priv_vfs_chown_othergid(root, !jail)", error, 0, 0); 148172106Srwatson if (!asroot && injail) 149172106Srwatson expect("priv_vfs_chown_othergid(!root, !jail)", error, -1, 150172106Srwatson EPERM); 151172106Srwatson if (!asroot && !injail) 152172106Srwatson expect("priv_vfs_chown_othergid(!root, !jail)", error, -1, 153172106Srwatson EPERM); 154172106Srwatson} 155172106Srwatson 156172106Srwatsonvoid 157172106Srwatsonpriv_vfs_chown_cleanup(int asroot, int injail, struct test *test) 158172106Srwatson{ 159172106Srwatson 160172106Srwatson if (fpath_initialized) { 161172106Srwatson (void)unlink(fpath); 162172106Srwatson fpath_initialized = 0; 163162271Srwatson } 164162271Srwatson} 165