2006-09-13 09:05:39 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 nCircle Network Security, Inc.
|
2007-09-09 23:08:39 +00:00
|
|
|
* Copyright (c) 2007 Robert N. M. Watson
|
2006-09-13 09:05:39 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Robert N. M. Watson for the TrustedBSD
|
|
|
|
* Project under contract to nCircle Network Security, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
|
|
|
|
* INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2007-09-09 23:08:39 +00:00
|
|
|
* Privilege test framework. Each test is encapsulated on a .c file
|
|
|
|
* exporting a function that implements the test. Each test is run from its
|
|
|
|
* own child process, and they are run in sequence one at a time.
|
2006-09-13 09:05:39 +00:00
|
|
|
*/
|
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/jail.h>
|
2006-09-13 09:05:39 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2006-09-13 09:05:39 +00:00
|
|
|
#include <err.h>
|
2007-09-09 23:08:39 +00:00
|
|
|
#include <errno.h>
|
2006-09-13 09:05:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
/*
|
2007-09-09 23:08:39 +00:00
|
|
|
* Registration table of privilege tests. Each test registers a name, a test
|
|
|
|
* function, and a cleanup function to run after the test has completed,
|
|
|
|
* regardless of success/failure.
|
2006-09-13 09:05:39 +00:00
|
|
|
*/
|
2007-09-09 23:08:39 +00:00
|
|
|
static struct test tests[] = {
|
|
|
|
{ "priv_acct_enable", priv_acct_setup, priv_acct_enable,
|
|
|
|
priv_acct_cleanup },
|
|
|
|
|
|
|
|
{ "priv_acct_disable", priv_acct_setup, priv_acct_disable,
|
|
|
|
priv_acct_cleanup },
|
|
|
|
|
|
|
|
{ "priv_acct_rotate", priv_acct_setup, priv_acct_rotate,
|
|
|
|
priv_acct_cleanup },
|
|
|
|
|
|
|
|
{ "priv_acct_noopdisable", priv_acct_setup, priv_acct_noopdisable,
|
|
|
|
priv_acct_cleanup },
|
|
|
|
|
|
|
|
{ "priv_adjtime_set", priv_adjtime_setup, priv_adjtime_set,
|
|
|
|
priv_adjtime_cleanup },
|
|
|
|
|
|
|
|
{ "priv_audit_submit", priv_audit_submit_setup, priv_audit_submit,
|
|
|
|
priv_audit_submit_cleanup },
|
|
|
|
|
|
|
|
{ "priv_audit_control", priv_audit_control_setup, priv_audit_control,
|
|
|
|
priv_audit_control_cleanup },
|
|
|
|
|
|
|
|
{ "priv_audit_getaudit", priv_audit_getaudit_setup,
|
|
|
|
priv_audit_getaudit, priv_audit_getaudit_cleanup },
|
|
|
|
|
|
|
|
{ "priv_audit_getaudit_addr", priv_audit_getaudit_setup,
|
|
|
|
priv_audit_getaudit_addr, priv_audit_getaudit_cleanup },
|
|
|
|
|
|
|
|
{ "priv_audit_setaudit", priv_audit_setaudit_setup,
|
|
|
|
priv_audit_setaudit, priv_audit_setaudit_cleanup },
|
|
|
|
|
|
|
|
{ "priv_audit_setaudit_addr", priv_audit_setaudit_setup,
|
|
|
|
priv_audit_setaudit_addr, priv_audit_setaudit_cleanup },
|
|
|
|
|
|
|
|
{ "priv_clock_settime", priv_clock_settime_setup, priv_clock_settime,
|
|
|
|
priv_clock_settime_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_setuid", priv_cred_setup, priv_cred_setuid,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_seteuid", priv_cred_setup, priv_cred_seteuid,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_setgid", priv_cred_setup, priv_cred_setgid,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_setegid", priv_cred_setup, priv_cred_setegid,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_setgroups", priv_cred_setup, priv_cred_setgroups,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_setreuid", priv_cred_setup, priv_cred_setreuid,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_setregid", priv_cred_setup, priv_cred_setregid,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_setresuid", priv_cred_setup, priv_cred_setresuid,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_cred_setresgid", priv_cred_setup, priv_cred_setresgid,
|
|
|
|
priv_cred_cleanup },
|
|
|
|
|
|
|
|
{ "priv_io", priv_io_setup, priv_io, priv_io_cleanup },
|
|
|
|
|
|
|
|
{ "priv_kenv_set", priv_kenv_set_setup, priv_kenv_set,
|
|
|
|
priv_kenv_set_cleanup },
|
|
|
|
|
|
|
|
{ "priv_kenv_unset", priv_kenv_unset_setup, priv_kenv_unset,
|
|
|
|
priv_kenv_unset_cleanup },
|
|
|
|
|
|
|
|
{ "priv_msgbuf_privonly", priv_msgbuf_privonly_setup,
|
|
|
|
priv_msgbuf_privonly, priv_msgbuf_cleanup },
|
|
|
|
|
|
|
|
{ "priv_msgbuf_unprivok", priv_msgbuf_unprivok_setup,
|
|
|
|
priv_msgbuf_unprivok, priv_msgbuf_cleanup },
|
|
|
|
|
|
|
|
{ "priv_netinet_raw", priv_netinet_raw_setup, priv_netinet_raw,
|
|
|
|
priv_netinet_raw_cleanup },
|
|
|
|
|
|
|
|
{ "priv_proc_setlogin", priv_proc_setlogin_setup, priv_proc_setlogin,
|
|
|
|
priv_proc_setlogin_cleanup },
|
|
|
|
|
|
|
|
{ "priv_proc_setrlimit_raisemax", priv_proc_setrlimit_setup,
|
|
|
|
priv_proc_setrlimit_raisemax, priv_proc_setrlimit_cleanup },
|
|
|
|
|
|
|
|
{ "priv_proc_setrlimit_raisecur", priv_proc_setrlimit_setup,
|
|
|
|
priv_proc_setrlimit_raisecur, priv_proc_setrlimit_cleanup },
|
|
|
|
|
|
|
|
{ "priv_proc_setrlimit_raisecur_nopriv", priv_proc_setrlimit_setup,
|
|
|
|
priv_proc_setrlimit_raisecur_nopriv,
|
|
|
|
priv_proc_setrlimit_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_curproc_normal", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_curproc_normal, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_curproc_idle", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_curproc_idle, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_curproc_realtime", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_curproc_realtime, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_myproc_normal", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_myproc_normal, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_myproc_idle", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_myproc_idle, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_myproc_realtime", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_myproc_realtime, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_aproc_normal", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_aproc_normal, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_aproc_idle", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_aproc_idle, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_rtprio_aproc_realtime", priv_sched_rtprio_setup,
|
|
|
|
priv_sched_rtprio_aproc_realtime, priv_sched_rtprio_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_setpriority_curproc", priv_sched_setpriority_setup,
|
|
|
|
priv_sched_setpriority_curproc, priv_sched_setpriority_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_setpriority_myproc", priv_sched_setpriority_setup,
|
|
|
|
priv_sched_setpriority_myproc, priv_sched_setpriority_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sched_setpriority_aproc", priv_sched_setpriority_setup,
|
|
|
|
priv_sched_setpriority_aproc, priv_sched_setpriority_cleanup },
|
|
|
|
|
|
|
|
{ "priv_settimeofday", priv_settimeofday_setup, priv_settimeofday,
|
|
|
|
priv_settimeofday_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sysctl_write", priv_sysctl_write_setup, priv_sysctl_write,
|
|
|
|
priv_sysctl_write_cleanup },
|
|
|
|
|
|
|
|
{ "priv_sysctl_writejail", priv_sysctl_write_setup,
|
|
|
|
priv_sysctl_writejail, priv_sysctl_write_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chflags_froot_uflags", priv_vfs_chflags_froot_setup,
|
|
|
|
priv_vfs_chflags_froot_uflags, priv_vfs_chflags_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chflags_froot_sflags", priv_vfs_chflags_froot_setup,
|
|
|
|
priv_vfs_chflags_froot_sflags, priv_vfs_chflags_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chflags_fowner_uflags", priv_vfs_chflags_fowner_setup,
|
|
|
|
priv_vfs_chflags_fowner_uflags, priv_vfs_chflags_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chflags_fowner_sflags", priv_vfs_chflags_fowner_setup,
|
|
|
|
priv_vfs_chflags_fowner_sflags, priv_vfs_chflags_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chflags_fother_uflags", priv_vfs_chflags_fother_setup,
|
|
|
|
priv_vfs_chflags_fother_uflags, priv_vfs_chflags_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chflags_fother_sflags", priv_vfs_chflags_fother_setup,
|
|
|
|
priv_vfs_chflags_fother_sflags, priv_vfs_chflags_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chmod_froot", priv_vfs_chmod_froot_setup,
|
|
|
|
priv_vfs_chmod_froot, priv_vfs_chmod_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chmod_fowner", priv_vfs_chmod_fowner_setup,
|
|
|
|
priv_vfs_chmod_fowner, priv_vfs_chmod_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chmod_fother", priv_vfs_chmod_fother_setup,
|
|
|
|
priv_vfs_chmod_fother, priv_vfs_chmod_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chown_uid", priv_vfs_chown_uid_setup, priv_vfs_chown_uid,
|
|
|
|
priv_vfs_chown_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chown_mygid", priv_vfs_chown_mygid_setup,
|
|
|
|
priv_vfs_chown_mygid, priv_vfs_chown_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chown_othergid", priv_vfs_chown_othergid_setup,
|
|
|
|
priv_vfs_chown_othergid, priv_vfs_chown_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_chroot", priv_vfs_chroot_setup, priv_vfs_chroot,
|
|
|
|
priv_vfs_chroot_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_clearsugid_chgrp", priv_vfs_clearsugid_setup,
|
|
|
|
priv_vfs_clearsugid_chgrp, priv_vfs_clearsugid_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_clearsugid_extattr", priv_vfs_clearsugid_setup,
|
|
|
|
priv_vfs_clearsugid_extattr, priv_vfs_clearsugid_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_clearsugid_write", priv_vfs_clearsugid_setup,
|
|
|
|
priv_vfs_clearsugid_write, priv_vfs_clearsugid_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_extattr_system", priv_vfs_extattr_system_setup,
|
|
|
|
priv_vfs_extattr_system, priv_vfs_extattr_system_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_fhopen", priv_vfs_fhopen_setup, priv_vfs_fhopen,
|
|
|
|
priv_vfs_fhopen_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_fhstat", priv_vfs_fhstat_setup, priv_vfs_fhstat,
|
|
|
|
priv_vfs_fhstat_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_fhstatfs", priv_vfs_fhstatfs_setup, priv_vfs_fhstatfs,
|
|
|
|
priv_vfs_fhstatfs_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_generation", priv_vfs_generation_setup,
|
|
|
|
priv_vfs_generation, priv_vfs_generation_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_getfh", priv_vfs_getfh_setup, priv_vfs_getfh,
|
|
|
|
priv_vfs_getfh_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_readwrite_fowner", priv_vfs_readwrite_fowner_setup,
|
|
|
|
priv_vfs_readwrite_fowner, priv_vfs_readwrite_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_readwrite_fgroup", priv_vfs_readwrite_fgroup_setup,
|
|
|
|
priv_vfs_readwrite_fgroup, priv_vfs_readwrite_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_readwrite_fother", priv_vfs_readwrite_fother_setup,
|
|
|
|
priv_vfs_readwrite_fother, priv_vfs_readwrite_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_setgid_fowner", priv_vfs_setgid_fowner_setup,
|
|
|
|
priv_vfs_setgid_fowner, priv_vfs_setgid_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_setgid_fother", priv_vfs_setgid_fother_setup,
|
|
|
|
priv_vfs_setgid_fother, priv_vfs_setgid_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_stickyfile_dir_fowner",
|
|
|
|
priv_vfs_stickyfile_dir_fowner_setup,
|
|
|
|
priv_vfs_stickyfile_dir_fowner,
|
|
|
|
priv_vfs_stickyfile_dir_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_stickyfile_dir_fother",
|
|
|
|
priv_vfs_stickyfile_dir_fother_setup,
|
|
|
|
priv_vfs_stickyfile_dir_fother,
|
|
|
|
priv_vfs_stickyfile_dir_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_stickyfile_file_fowner",
|
|
|
|
priv_vfs_stickyfile_file_fowner_setup,
|
|
|
|
priv_vfs_stickyfile_file_fowner,
|
|
|
|
priv_vfs_stickyfile_file_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_stickyfile_file_fother",
|
|
|
|
priv_vfs_stickyfile_file_fother_setup,
|
|
|
|
priv_vfs_stickyfile_file_fother,
|
|
|
|
priv_vfs_stickyfile_file_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_utimes_froot", priv_vfs_utimes_froot_setup,
|
|
|
|
priv_vfs_utimes_froot, priv_vfs_utimes_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_utimes_froot_null", priv_vfs_utimes_froot_setup,
|
|
|
|
priv_vfs_utimes_froot_null, priv_vfs_utimes_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_utimes_fowner", priv_vfs_utimes_fowner_setup,
|
|
|
|
priv_vfs_utimes_fowner, priv_vfs_utimes_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_utimes_fowner_null", priv_vfs_utimes_fowner_setup,
|
|
|
|
priv_vfs_utimes_fowner_null, priv_vfs_utimes_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_utimes_fother", priv_vfs_utimes_fother_setup,
|
|
|
|
priv_vfs_utimes_fother, priv_vfs_utimes_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vfs_utimes_fother_null", priv_vfs_utimes_fother_setup,
|
|
|
|
priv_vfs_utimes_fother_null, priv_vfs_utimes_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vm_madv_protect", priv_vm_madv_protect_setup,
|
|
|
|
priv_vm_madv_protect, priv_vm_madv_protect_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vm_mlock", priv_vm_mlock_setup, priv_vm_mlock,
|
|
|
|
priv_vm_mlock_cleanup },
|
|
|
|
|
|
|
|
{ "priv_vm_munlock", priv_vm_munlock_setup, priv_vm_munlock,
|
|
|
|
priv_vm_munlock_cleanup },
|
|
|
|
|
|
|
|
};
|
|
|
|
static int tests_count = sizeof(tests) / sizeof(struct test);
|
|
|
|
|
|
|
|
void
|
|
|
|
expect(const char *test, int error, int expected_error, int expected_errno)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (error == 0) {
|
|
|
|
if (expected_error != 0)
|
|
|
|
warnx("%s: returned 0", test);
|
|
|
|
} else {
|
|
|
|
if (expected_error == 0)
|
|
|
|
warn("%s: returned (%d, %d)", test, error, errno);
|
|
|
|
else if (expected_errno != errno)
|
|
|
|
warn("%s: returned (%d, %d)", test, error, errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-13 09:05:39 +00:00
|
|
|
void
|
2007-09-09 23:08:39 +00:00
|
|
|
setup_dir(const char *test, char *dpathp, uid_t uid, gid_t gid, mode_t mode)
|
2006-09-13 09:05:39 +00:00
|
|
|
{
|
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
strcpy(dpathp, "/tmp/priv.XXXXXXXXXXX");
|
|
|
|
if (mkdtemp(dpathp) == NULL)
|
|
|
|
err(-1, "test %s: mkdtemp", test);
|
|
|
|
|
|
|
|
if (chown(dpathp, uid, gid) < 0)
|
|
|
|
err(-1, "test %s: chown(%s, %d, %d)", test, dpathp, uid,
|
|
|
|
gid);
|
|
|
|
|
|
|
|
if (chmod(dpathp, mode) < 0)
|
|
|
|
err(-1, "test %s: chmod(%s, 0%o)", test, dpathp, mode);
|
2006-09-13 09:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-09 23:08:39 +00:00
|
|
|
setup_file(const char *test, char *fpathp, uid_t uid, gid_t gid, mode_t mode)
|
2006-09-13 09:05:39 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
strcpy(fpathp, "/tmp/priv.XXXXXXXXXXX");
|
|
|
|
fd = mkstemp(fpathp);
|
|
|
|
if (fd < 0)
|
2007-09-09 23:08:39 +00:00
|
|
|
err(-1, "test %s: mkstemp", test);
|
2006-09-13 09:05:39 +00:00
|
|
|
|
|
|
|
if (fchown(fd, uid, gid) < 0)
|
2007-09-09 23:08:39 +00:00
|
|
|
err(-1, "test %s: fchown(%s, %d, %d)", test, fpathp, uid,
|
|
|
|
gid);
|
2006-09-13 09:05:39 +00:00
|
|
|
|
|
|
|
if (fchmod(fd, mode) < 0)
|
2007-09-09 23:08:39 +00:00
|
|
|
err(-1, "test %s: chmod(%s, 0%o)", test, fpathp, mode);
|
2006-09-13 09:05:39 +00:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-09 23:08:39 +00:00
|
|
|
* Irrevocably set credentials to specific uid and gid.
|
2006-09-13 09:05:39 +00:00
|
|
|
*/
|
2007-09-09 23:08:39 +00:00
|
|
|
static void
|
|
|
|
set_creds(const char *test, uid_t uid, gid_t gid)
|
2006-09-13 09:05:39 +00:00
|
|
|
{
|
2007-09-09 23:08:39 +00:00
|
|
|
gid_t gids[1] = { gid };
|
2006-09-13 09:05:39 +00:00
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
if (setgid(gid) < 0)
|
|
|
|
err(-1, "test %s: setegid(%d)", test, gid);
|
|
|
|
if (setgroups(sizeof(gids)/sizeof(gid_t), gids) < 0)
|
|
|
|
err(-1, "test %s: setgroups(%d)", test, gid);
|
|
|
|
if (setuid(uid) < 0)
|
|
|
|
err(-1, "test %s: seteuid(%d)", test, uid);
|
2006-09-13 09:05:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
static void
|
|
|
|
enter_jail(const char *test)
|
2006-09-13 09:05:39 +00:00
|
|
|
{
|
2007-09-09 23:08:39 +00:00
|
|
|
struct jail j;
|
2006-09-13 09:05:39 +00:00
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
bzero(&j, sizeof(j));
|
|
|
|
j.version = 0;
|
|
|
|
j.path = "/";
|
|
|
|
j.hostname = "test";
|
|
|
|
j.ip_number = htonl(INADDR_LOOPBACK);
|
|
|
|
if (jail(&j) < 0)
|
|
|
|
err(-1, "test %s: jail", test);
|
2006-09-13 09:05:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
static void
|
|
|
|
run_child(struct test *test, int asroot, int injail)
|
2006-09-13 09:05:39 +00:00
|
|
|
{
|
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
setprogname(test->t_name);
|
|
|
|
if (injail)
|
|
|
|
enter_jail(test->t_name);
|
|
|
|
if (!asroot)
|
|
|
|
set_creds(test->t_name, UID_OWNER, GID_OWNER);
|
|
|
|
test->t_test_func(asroot, injail, test);
|
2006-09-13 09:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-09-09 23:08:39 +00:00
|
|
|
* Run a test in a particular credential context -- always call the setup and
|
|
|
|
* cleanup routines; if setup succeeds, also run the test. Test cleanup must
|
|
|
|
* handle cases where the setup has failed, so may need to maintain their own
|
|
|
|
* state in order to know what needs cleaning up (such as whether temporary
|
|
|
|
* files were created).
|
2006-09-13 09:05:39 +00:00
|
|
|
*/
|
|
|
|
static void
|
2007-09-09 23:08:39 +00:00
|
|
|
run(struct test *test, int asroot, int injail)
|
2006-09-13 09:05:39 +00:00
|
|
|
{
|
|
|
|
pid_t childpid, pid;
|
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
if (test->t_setup_func != NULL) {
|
|
|
|
if ((test->t_setup_func)(asroot, injail, test) != 0) {
|
|
|
|
warnx("run(%s, %d, %d) setup failed", test->t_name,
|
|
|
|
asroot, injail);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2006-09-13 09:05:39 +00:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
childpid = fork();
|
2007-09-09 23:08:39 +00:00
|
|
|
if (childpid == -1) {
|
|
|
|
warn("run(%s, %d, %d) fork failed", test->t_name, asroot,
|
|
|
|
injail);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2006-09-13 09:05:39 +00:00
|
|
|
if (childpid == 0) {
|
2007-09-09 23:08:39 +00:00
|
|
|
run_child(test, asroot, injail);
|
2006-09-13 09:05:39 +00:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
while (1) {
|
|
|
|
pid = waitpid(childpid, NULL, 0);
|
|
|
|
if (pid == -1)
|
2007-09-09 23:08:39 +00:00
|
|
|
warn("test: waitpid %s", test->t_name);
|
2006-09-13 09:05:39 +00:00
|
|
|
if (pid == childpid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2007-09-09 23:08:39 +00:00
|
|
|
cleanup:
|
|
|
|
if (test->t_cleanup_func != NULL)
|
|
|
|
test->t_cleanup_func(asroot, injail, test);
|
2006-09-13 09:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2007-09-09 23:08:39 +00:00
|
|
|
int i;
|
2006-09-13 09:05:39 +00:00
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
/*
|
|
|
|
* This test suite will need to become quite a bit more enlightened
|
|
|
|
* if the notion of privilege is truly separated from root, as tests
|
|
|
|
* make assumptions about when privilege will be present. In
|
|
|
|
* particular, VFS-related tests need to manage uids in order to
|
|
|
|
* force the use of privilege, and will likely need checking.
|
|
|
|
*/
|
|
|
|
if (getuid() != 0 && geteuid() != 0)
|
|
|
|
errx(-1, "priv: must be run as root");
|
2006-09-13 09:05:39 +00:00
|
|
|
|
2007-09-09 23:08:39 +00:00
|
|
|
/*
|
|
|
|
* Run each test four times, varying whether the process is running
|
|
|
|
* as root and in jail in order to test all possible combinations.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < tests_count; i++) {
|
|
|
|
run(&tests[i], 0, 0);
|
|
|
|
run(&tests[i], 0, 1);
|
|
|
|
run(&tests[i], 1, 0);
|
|
|
|
run(&tests[i], 1, 1);
|
|
|
|
}
|
2006-09-13 09:05:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|