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: releng/10.3/tools/regression/priv/priv_vfs_generation.c 172106 2007-09-09 23:08:39Z rwatson $ 31162271Srwatson */ 32162271Srwatson 33162271Srwatson/* 34162271Srwatson * Confirm that a generation number isn't returned by stat() when not running 35162271Srwatson * with privilege. In order to differentiate between a generation of 0 and 36162271Srwatson * a generation not being returned, we have to create a temporary file known 37172106Srwatson * to have a non-0 generation. We try up to MAX_TRIES times, and then give 38172106Srwatson * up, which is non-ideal, but better than not testing for a problem. 39162271Srwatson */ 40162271Srwatson 41162271Srwatson#include <sys/stat.h> 42162271Srwatson 43162271Srwatson#include <err.h> 44162271Srwatson#include <errno.h> 45162271Srwatson#include <string.h> 46162271Srwatson#include <unistd.h> 47162271Srwatson 48162271Srwatson#include "main.h" 49162271Srwatson 50172106Srwatsonstatic char fpath[1024]; 51172106Srwatsonstatic int fpath_initialized; 52172106Srwatson 53172106Srwatson#define MAX_TRIES 100 54172106Srwatson 55172106Srwatsonint 56172106Srwatsonpriv_vfs_generation_setup(int asroot, int injail, struct test *test) 57162271Srwatson{ 58162271Srwatson struct stat sb; 59172106Srwatson int i; 60162271Srwatson 61162271Srwatson /* 62172106Srwatson * The kernel zeros the generation number field when an unprivileged 63172106Srwatson * user stats a file. In order to distinguish the two cases, we 64172106Srwatson * therefore require a file that we know has a non-zero generation 65172106Srwatson * number. We try up to MAX_TRIES times and otherwise fail. 66162271Srwatson */ 67172106Srwatson for (i = 0; i < MAX_TRIES; i++) { 68172106Srwatson setup_file("priv_vfs_generation_setup: fpath", fpath, 69172106Srwatson UID_ROOT, GID_WHEEL, 0644); 70172106Srwatson if (stat(fpath, &sb) < 0) { 71172106Srwatson warn("priv_vfs_generation_setup: fstat(%s)", fpath); 72172106Srwatson (void)unlink(fpath); 73172106Srwatson return (-1); 74162271Srwatson } 75172106Srwatson if (sb.st_gen != 0) { 76172106Srwatson fpath_initialized = 1; 77172106Srwatson return (0); 78172106Srwatson } 79162271Srwatson } 80172106Srwatson warnx("priv_vfs_generation_setup: unable to create gen file"); 81172106Srwatson return (-1); 82172106Srwatson} 83162271Srwatson 84172106Srwatsonvoid 85172106Srwatsonpriv_vfs_generation(int asroot, int injail, struct test *test) 86172106Srwatson{ 87172106Srwatson struct stat sb; 88172106Srwatson int error; 89162271Srwatson 90172106Srwatson error = stat(fpath, &sb); 91172106Srwatson if (error < 0) 92172106Srwatson warn("priv_vfs_generation(asroot, injail) stat"); 93162271Srwatson 94172106Srwatson if (sb.st_gen == 0) { 95172106Srwatson error = -1; 96172106Srwatson errno = EPERM; 97172106Srwatson } else 98172106Srwatson error = 0; 99172106Srwatson if (asroot && injail) 100172106Srwatson expect("priv_vfs_generation(asroot, injail)", error, -1, 101172106Srwatson EPERM); 102172106Srwatson if (asroot && !injail) 103172106Srwatson expect("priv_vfs_generation(asroot, !injail)", error, 0, 0); 104172106Srwatson if (!asroot && injail) 105172106Srwatson expect("priv_vfs_generation(!asroot, injail)", error, -1, 106172106Srwatson EPERM); 107172106Srwatson if (!asroot && !injail) 108172106Srwatson expect("priv_vfs_generation(!asroot, !injail)", error, -1, 109172106Srwatson EPERM); 110172106Srwatson} 111162271Srwatson 112172106Srwatsonvoid 113172106Srwatsonpriv_vfs_generation_cleanup(int asroot, int injail, struct test *test) 114172106Srwatson{ 115162271Srwatson 116172106Srwatson if (fpath_initialized) { 117172106Srwatson (void)unlink(fpath); 118172106Srwatson fpath_initialized = 0; 119172106Srwatson } 120162271Srwatson} 121