2 * Copyright (c) 2009-2011 Robert N. M. Watson
3 * Copyright (c) 2011 Jonathan Anderson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Test that fcntl works in capability mode.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/capsicum.h>
37 #include <sys/errno.h>
40 #include <sys/socket.h>
42 #include <sys/sysctl.h>
53 /* A filename->descriptor mapping. */
60 * Ensure that fcntl() works consistently for both regular file descriptors and
61 * capability-wrapped ones.
67 cap_rights_t rights = CAP_READ | CAP_FCNTL;
70 * Open some files of different types, and wrap them in capabilities.
73 { "file", open("/etc/passwd", O_RDONLY) },
74 { "socket", socket(PF_LOCAL, SOCK_STREAM, 0) },
75 { "SHM", shm_open(SHM_ANON, O_RDWR, 0600) },
77 REQUIRE(files[0].f_fd);
78 REQUIRE(files[1].f_fd);
79 REQUIRE(files[2].f_fd);
82 { "file cap", cap_new(files[0].f_fd, rights) },
83 { "socket cap", cap_new(files[1].f_fd, rights) },
84 { "SHM cap", cap_new(files[2].f_fd, rights) },
86 REQUIRE(caps[0].f_fd);
87 REQUIRE(caps[1].f_fd);
88 REQUIRE(caps[2].f_fd);
95 const size_t len = sizeof(all) / sizeof(struct fd);
100 * Ensure that we can fcntl() all the files that we opened above.
102 for (size_t i = 0; i < len; i++)
104 struct fd f = all[i];
107 CHECK_SYSCALL_SUCCEEDS(fcntl, f.f_fd, F_GETFL, 0);
108 REQUIRE(cap = cap_new(f.f_fd, CAP_READ));
109 if (fcntl(f.f_fd, F_GETFL, 0) == -1)
110 FAIL("Error calling fcntl('%s', F_GETFL)", f.f_name);
112 CHECK_NOTCAPABLE(fcntl, cap, F_GETFL, 0);