Path: blob/main/tools/regression/security/cap_test/cap_test_fcntl.c
48266 views
/*-1* Copyright (c) 2009-2011 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 that fcntl works in capability mode.29*/3031#include <sys/types.h>32#include <sys/capsicum.h>33#include <sys/errno.h>34#include <sys/ipc.h>35#include <sys/mman.h>36#include <sys/socket.h>37#include <sys/stat.h>38#include <sys/sysctl.h>39#include <sys/wait.h>4041#include <err.h>42#include <fcntl.h>43#include <stdio.h>44#include <stdlib.h>45#include <unistd.h>4647#include "cap_test.h"4849/* A filename->descriptor mapping. */50struct fd {51char *f_name;52int f_fd;53};5455/*56* Ensure that fcntl() works consistently for both regular file descriptors and57* capability-wrapped ones.58*/59int60test_fcntl(void)61{62int success = PASSED;63cap_rights_t rights = CAP_READ | CAP_FCNTL;6465/*66* Open some files of different types, and wrap them in capabilities.67*/68struct fd files[] = {69{ "file", open("/etc/passwd", O_RDONLY) },70{ "socket", socket(PF_LOCAL, SOCK_STREAM, 0) },71{ "SHM", shm_open(SHM_ANON, O_RDWR, 0600) },72};73REQUIRE(files[0].f_fd);74REQUIRE(files[1].f_fd);75REQUIRE(files[2].f_fd);7677struct fd caps[] = {78{ "file cap", cap_new(files[0].f_fd, rights) },79{ "socket cap", cap_new(files[1].f_fd, rights) },80{ "SHM cap", cap_new(files[2].f_fd, rights) },81};82REQUIRE(caps[0].f_fd);83REQUIRE(caps[1].f_fd);84REQUIRE(caps[2].f_fd);8586struct fd all[] = {87files[0], caps[0],88files[1], caps[1],89files[2], caps[2],90};91const size_t len = sizeof(all) / sizeof(struct fd);9293REQUIRE(cap_enter());9495/*96* Ensure that we can fcntl() all the files that we opened above.97*/98for (size_t i = 0; i < len; i++)99{100struct fd f = all[i];101int cap;102103CHECK_SYSCALL_SUCCEEDS(fcntl, f.f_fd, F_GETFL, 0);104REQUIRE(cap = cap_new(f.f_fd, CAP_READ));105if (fcntl(f.f_fd, F_GETFL, 0) == -1)106FAIL("Error calling fcntl('%s', F_GETFL)", f.f_name);107else108CHECK_NOTCAPABLE(fcntl, cap, F_GETFL, 0);109}110111return (success);112}113114115116