1 /*-
2 * Copyright (c) 2009-2011 Robert N. M. Watson
3 * Copyright (c) 2011 Jonathan Anderson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * Test that fcntl works in capability mode.
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/types.h>
36 #include <sys/capsicum.h>
37 #include <sys/errno.h>
38 #include <sys/ipc.h>
39 #include <sys/mman.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 #include <sys/sysctl.h>
43 #include <sys/wait.h>
44
45 #include <err.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50
51 #include "cap_test.h"
52
53 /* A filename->descriptor mapping. */
54 struct fd {
55 char *f_name;
56 int f_fd;
57 };
58
59 /*
60 * Ensure that fcntl() works consistently for both regular file descriptors and
61 * capability-wrapped ones.
62 */
63 int
test_fcntl(void)64 test_fcntl(void)
65 {
66 int success = PASSED;
67 cap_rights_t rights = CAP_READ | CAP_FCNTL;
68
69 /*
70 * Open some files of different types, and wrap them in capabilities.
71 */
72 struct fd files[] = {
73 { "file", open("/etc/passwd", O_RDONLY) },
74 { "socket", socket(PF_LOCAL, SOCK_STREAM, 0) },
75 { "SHM", shm_open(SHM_ANON, O_RDWR, 0600) },
76 };
77 REQUIRE(files[0].f_fd);
78 REQUIRE(files[1].f_fd);
79 REQUIRE(files[2].f_fd);
80
81 struct fd caps[] = {
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) },
85 };
86 REQUIRE(caps[0].f_fd);
87 REQUIRE(caps[1].f_fd);
88 REQUIRE(caps[2].f_fd);
89
90 struct fd all[] = {
91 files[0], caps[0],
92 files[1], caps[1],
93 files[2], caps[2],
94 };
95 const size_t len = sizeof(all) / sizeof(struct fd);
96
97 REQUIRE(cap_enter());
98
99 /*
100 * Ensure that we can fcntl() all the files that we opened above.
101 */
102 for (size_t i = 0; i < len; i++)
103 {
104 struct fd f = all[i];
105 int cap;
106
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);
111 else
112 CHECK_NOTCAPABLE(fcntl, cap, F_GETFL, 0);
113 }
114
115 return (success);
116 }
117
118