priv_sysctl_write.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_sysctl_write.c 162271 2006-09-13 09:05:39Z rwatson $ 30162271Srwatson */ 31162271Srwatson 32162271Srwatson/* 33162271Srwatson * Test that sysctls can only be written with privilege by trying first with, 34162271Srwatson * then without privilege. Do this by first reading, then setting the 35162271Srwatson * hostname as a no-op. 36162271Srwatson */ 37162271Srwatson 38162271Srwatson#include <sys/types.h> 39162271Srwatson#include <sys/sysctl.h> 40162271Srwatson 41162271Srwatson#include <err.h> 42162271Srwatson#include <errno.h> 43162271Srwatson#include <string.h> 44162271Srwatson#include <unistd.h> 45162271Srwatson 46162271Srwatson#include "main.h" 47162271Srwatson 48162271Srwatson#define KERN_HOSTNAME_STRING "kern.hostname" 49162271Srwatson 50162271Srwatsonvoid 51162271Srwatsonpriv_sysctl_write(void) 52162271Srwatson{ 53162271Srwatson char buffer[1024]; 54162271Srwatson size_t len; 55162271Srwatson int error; 56162271Srwatson 57162271Srwatson assert_root(); 58162271Srwatson 59162271Srwatson /* 60162271Srwatson * First query the current value. 61162271Srwatson */ 62162271Srwatson len = sizeof(buffer); 63162271Srwatson error = sysctlbyname(KERN_HOSTNAME_STRING, buffer, &len, NULL, 0); 64162271Srwatson if (error) 65162271Srwatson err(-1, "sysctlbyname(\"%s\") query", KERN_HOSTNAME_STRING); 66162271Srwatson 67162271Srwatson /* 68162271Srwatson * Now try to set with privilege. 69162271Srwatson */ 70162271Srwatson error = sysctlbyname(KERN_HOSTNAME_STRING, NULL, NULL, buffer, 71162271Srwatson strlen(buffer)); 72162271Srwatson if (error) 73162271Srwatson err(-1, "sysctlbyname(\"%s\") set as root", 74162271Srwatson KERN_HOSTNAME_STRING); 75162271Srwatson 76162271Srwatson /* 77162271Srwatson * Now without privilege. 78162271Srwatson */ 79162271Srwatson set_euid(UID_OTHER); 80162271Srwatson 81162271Srwatson error = sysctlbyname(KERN_HOSTNAME_STRING, NULL, NULL, buffer, 82162271Srwatson strlen(buffer)); 83162271Srwatson if (error == 0) 84162271Srwatson errx(-1, "sysctlbyname(\"%s\") succeeded as !root", 85162271Srwatson KERN_HOSTNAME_STRING); 86162271Srwatson if (errno != EPERM) 87162271Srwatson err(-1, "sysctlbyname(\"%s\") wrong errno %d", 88162271Srwatson KERN_HOSTNAME_STRING, errno); 89162271Srwatson} 90