1 /*-
2  * Copyright (c) 2016 Mariusz Zaborski <oshogbo@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _CAPSICUM_HELPERS_H_
28 #define	_CAPSICUM_HELPERS_H_
29 
30 #include <sys/param.h>
31 #include <sys/capsicum.h>
32 #include <sys/ioctl.h>
33 
34 #include <errno.h>
35 #include <nl_types.h>
36 #include <termios.h>
37 #include <time.h>
38 #include <unistd.h>
39 
40 #include <libcasper.h>
41 
42 #define	CAPH_IGNORE_EBADF	0x0001
43 #define	CAPH_READ		0x0002
44 #define	CAPH_WRITE		0x0004
45 #define	CAPH_LOOKUP		0x0008
46 
47 __BEGIN_DECLS
48 
49 static const unsigned long caph_stream_cmds[] =
50     {
51 #ifdef TIOCGETA
52 	TIOCGETA,
53 #endif
54 #ifdef TIOCGWINSZ
55 	TIOCGWINSZ,
56 #endif
57 #ifdef FIODTYPE
58 	FIODTYPE,
59 #endif
60     };
61 static const uint32_t caph_stream_fcntls = CAP_FCNTL_GETFL;
62 
63 static __inline void
64 caph_stream_rights(cap_rights_t *rights, int flags)
65 {
66 
67 	cap_rights_init(rights, CAP_EVENT, CAP_FCNTL, CAP_FSTAT,
68 	    CAP_IOCTL, CAP_SEEK);
69 
70 	if ((flags & CAPH_READ) != 0)
71 		cap_rights_set(rights, CAP_READ);
72 	if ((flags & CAPH_WRITE) != 0)
73 		cap_rights_set(rights, CAP_WRITE);
74 	if ((flags & CAPH_LOOKUP) != 0)
75 		cap_rights_set(rights, CAP_LOOKUP);
76 }
77 
78 static __inline int
79 caph_limit_stream(int fd, int flags)
80 {
81 	cap_rights_t rights;
82 
83 	caph_stream_rights(&rights, flags);
84 	if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS) {
85 		if (errno == EBADF && (flags & CAPH_IGNORE_EBADF) != 0)
86 			return (0);
87 		return (-1);
88 	}
89 
90 	if (cap_ioctls_limit(fd, caph_stream_cmds,
91 	    nitems(caph_stream_cmds)) < 0 && errno != ENOSYS)
92 		return (-1);
93 
94 	if (cap_fcntls_limit(fd, caph_stream_fcntls) < 0 && errno != ENOSYS)
95 		return (-1);
96 
97 	return (0);
98 }
99 
100 static __inline int
101 caph_limit_stdin(void)
102 {
103 
104 	return (caph_limit_stream(STDIN_FILENO, CAPH_READ));
105 }
106 
107 static __inline int
108 caph_limit_stderr(void)
109 {
110 
111 	return (caph_limit_stream(STDERR_FILENO, CAPH_WRITE));
112 }
113 
114 static __inline int
115 caph_limit_stdout(void)
116 {
117 
118 	return (caph_limit_stream(STDOUT_FILENO, CAPH_WRITE));
119 }
120 
121 static __inline int
122 caph_limit_stdio(void)
123 {
124 	const int iebadf = CAPH_IGNORE_EBADF;
125 
126 	if (caph_limit_stream(STDIN_FILENO, CAPH_READ | iebadf) == -1 ||
127 	    caph_limit_stream(STDOUT_FILENO, CAPH_WRITE | iebadf) == -1 ||
128 	    caph_limit_stream(STDERR_FILENO, CAPH_WRITE | iebadf) == -1)
129 		return (-1);
130 	return (0);
131 }
132 
133 static __inline void
134 caph_cache_tzdata(void)
135 {
136 
137 	tzset();
138 }
139 
140 static __inline void
141 caph_cache_catpages(void)
142 {
143 
144 	(void)catopen("libc", NL_CAT_LOCALE);
145 }
146 
147 static __inline int
148 caph_enter(void)
149 {
150 
151 	if (cap_enter() < 0 && errno != ENOSYS)
152 		return (-1);
153 
154 	return (0);
155 }
156 
157 static __inline int
158 caph_rights_limit(int fd, const cap_rights_t *rights)
159 {
160 
161 	if (cap_rights_limit(fd, rights) < 0 && errno != ENOSYS)
162 		return (-1);
163 
164 	return (0);
165 }
166 
167 static __inline int
168 caph_ioctls_limit(int fd, const unsigned long *cmds, size_t ncmds)
169 {
170 
171 	if (cap_ioctls_limit(fd, cmds, ncmds) < 0 && errno != ENOSYS)
172 		return (-1);
173 
174 	return (0);
175 }
176 
177 static __inline int
178 caph_fcntls_limit(int fd, uint32_t fcntlrights)
179 {
180 
181 	if (cap_fcntls_limit(fd, fcntlrights) < 0 && errno != ENOSYS)
182 		return (-1);
183 
184 	return (0);
185 }
186 
187 static __inline int
188 caph_enter_casper(void)
189 {
190 
191 	return (CASPER_SUPPORT == 0 ? 0 : caph_enter());
192 }
193 
194 __END_DECLS
195 
196 #endif /* _CAPSICUM_HELPERS_H_ */
197