Path: blob/main/tools/regression/security/cap_test/cap_test_capmode.c
48266 views
/*-1* Copyright (c) 2008-2009 Robert N. M. Watson2* Copyright (c) 2011 Jonathan Anderson3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627/*28* Test routines to make sure a variety of system calls are or are not29* available in capability mode. The goal is not to see if they work, just30* whether or not they return the expected ECAPMODE.31*/3233#include <sys/param.h>34#include <sys/capsicum.h>35#include <sys/errno.h>36#include <sys/mman.h>37#include <sys/mount.h>38#include <sys/socket.h>39#include <sys/stat.h>40#include <sys/wait.h>4142#include <machine/sysarch.h>4344#include <err.h>45#include <fcntl.h>46#include <stdlib.h>47#include <string.h>48#include <unistd.h>4950#include "cap_test.h"5152#define CHECK_SYSCALL_VOID_NOT_ECAPMODE(syscall, ...) do { \53errno = 0; \54(void)syscall(__VA_ARGS__); \55if (errno == ECAPMODE) \56FAIL("capmode: %s failed with ECAPMODE", #syscall); \57} while (0)5859int60test_capmode(void)61{62struct statfs statfs;63struct stat sb;64long sysarch_arg = 0;65int fd_close, fd_dir, fd_file, fd_socket, fd2[2];66int success = PASSED;67pid_t pid, wpid;68char ch;6970/* Open some files to play with. */71REQUIRE(fd_file = open("/tmp/cap_capmode", O_RDWR|O_CREAT, 0644));72REQUIRE(fd_close = open("/dev/null", O_RDWR));73REQUIRE(fd_dir = open("/tmp", O_RDONLY));74REQUIRE(fd_socket = socket(PF_INET, SOCK_DGRAM, 0));7576/* Enter capability mode. */77REQUIRE(cap_enter());7879/*80* System calls that are not permitted in capability mode.81*/82CHECK_CAPMODE(access, "/tmp/cap_capmode_access", F_OK);83CHECK_CAPMODE(acct, "/tmp/cap_capmode_acct");84CHECK_CAPMODE(bind, PF_INET, NULL, 0);85CHECK_CAPMODE(chdir, "/tmp/cap_capmode_chdir");86CHECK_CAPMODE(chflags, "/tmp/cap_capmode_chflags", UF_NODUMP);87CHECK_CAPMODE(chmod, "/tmp/cap_capmode_chmod", 0644);88CHECK_CAPMODE(chown, "/tmp/cap_capmode_chown", -1, -1);89CHECK_CAPMODE(chroot, "/tmp/cap_capmode_chroot");90CHECK_CAPMODE(connect, PF_INET, NULL, 0);91CHECK_CAPMODE(creat, "/tmp/cap_capmode_creat", 0644);92CHECK_CAPMODE(fchdir, fd_dir);93CHECK_CAPMODE(getfsstat, &statfs, sizeof(statfs), MNT_NOWAIT);94CHECK_CAPMODE(link, "/tmp/foo", "/tmp/bar");95CHECK_CAPMODE(lstat, "/tmp/cap_capmode_lstat", &sb);96CHECK_CAPMODE(mknod, "/tmp/capmode_mknod", 06440, 0);97CHECK_CAPMODE(mount, "procfs", "/not_mounted", 0, NULL);98CHECK_CAPMODE(open, "/dev/null", O_RDWR);99CHECK_CAPMODE(readlink, "/tmp/cap_capmode_readlink", NULL, 0);100CHECK_CAPMODE(revoke, "/tmp/cap_capmode_revoke");101CHECK_CAPMODE(stat, "/tmp/cap_capmode_stat", &sb);102CHECK_CAPMODE(symlink,103"/tmp/cap_capmode_symlink_from",104"/tmp/cap_capmode_symlink_to");105CHECK_CAPMODE(unlink, "/tmp/cap_capmode_unlink");106CHECK_CAPMODE(unmount, "/not_mounted", 0);107108/*109* System calls that are permitted in capability mode.110*/111CHECK_SYSCALL_SUCCEEDS(close, fd_close);112CHECK_SYSCALL_SUCCEEDS(dup, fd_file);113CHECK_SYSCALL_SUCCEEDS(fstat, fd_file, &sb);114CHECK_SYSCALL_SUCCEEDS(lseek, fd_file, 0, SEEK_SET);115CHECK_SYSCALL_SUCCEEDS(msync, &fd_file, 8192, MS_ASYNC);116CHECK_SYSCALL_SUCCEEDS(profil, NULL, 0, 0, 0);117CHECK_SYSCALL_SUCCEEDS(read, fd_file, &ch, sizeof(ch));118CHECK_SYSCALL_SUCCEEDS(recvfrom, fd_socket, NULL, 0, 0, NULL, NULL);119CHECK_SYSCALL_SUCCEEDS(setuid, getuid());120CHECK_SYSCALL_SUCCEEDS(write, fd_file, &ch, sizeof(ch));121122/*123* These calls will fail for lack of e.g. a proper name to send to,124* but they are allowed in capability mode, so errno != ECAPMODE.125*/126CHECK_NOT_CAPMODE(accept, fd_socket, NULL, NULL);127CHECK_NOT_CAPMODE(getpeername, fd_socket, NULL, NULL);128CHECK_NOT_CAPMODE(getsockname, fd_socket, NULL, NULL);129CHECK_NOT_CAPMODE(fchflags, fd_file, UF_NODUMP);130CHECK_NOT_CAPMODE(recvmsg, fd_socket, NULL, 0);131CHECK_NOT_CAPMODE(sendmsg, fd_socket, NULL, 0);132CHECK_NOT_CAPMODE(sendto, fd_socket, NULL, 0, 0, NULL, 0);133134/*135* System calls which should be allowed in capability mode, but which136* don't return errors, and are thus difficult to check.137*138* We will try anyway, by checking errno.139*/140CHECK_SYSCALL_VOID_NOT_ECAPMODE(getegid);141CHECK_SYSCALL_VOID_NOT_ECAPMODE(geteuid);142CHECK_SYSCALL_VOID_NOT_ECAPMODE(getgid);143CHECK_SYSCALL_VOID_NOT_ECAPMODE(getpid);144CHECK_SYSCALL_VOID_NOT_ECAPMODE(getppid);145CHECK_SYSCALL_VOID_NOT_ECAPMODE(getuid);146147/*148* Finally, tests for system calls that don't fit the pattern very well.149*/150pid = fork();151if (pid >= 0) {152if (pid == 0) {153exit(0);154} else if (pid > 0) {155wpid = waitpid(pid, NULL, 0);156if (wpid < 0) {157if (errno != ECAPMODE)158FAIL("capmode:waitpid");159} else160FAIL("capmode:waitpid succeeded");161}162} else163FAIL("capmode:fork");164165if (getlogin() == NULL)166FAIL("test_sycalls:getlogin %d", errno);167168if (getsockname(fd_socket, NULL, NULL) < 0) {169if (errno == ECAPMODE)170FAIL("capmode:getsockname");171}172173/* XXXRW: ktrace */174175if (pipe(fd2) == 0) {176close(fd2[0]);177close(fd2[1]);178} else if (errno == ECAPMODE)179FAIL("capmode:pipe");180181/* XXXRW: ptrace. */182183/* sysarch() is, by definition, architecture-dependent */184#if defined (__amd64__) || defined (__i386__)185CHECK_CAPMODE(sysarch, I386_SET_IOPERM, &sysarch_arg);186#else187/* XXXJA: write a test for arm */188FAIL("capmode:no sysarch() test for current architecture");189#endif190191/* XXXRW: No error return from sync(2) to test. */192193return (success);194}195196197