]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libcasper/libcasper.c
Upgrade to OpenSSH 6.5p1.
[FreeBSD/FreeBSD.git] / lib / libcasper / libcasper.c
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34 #include <sys/capability.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/un.h>
39
40 #include <assert.h>
41 #include <dirent.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdbool.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <strings.h>
50 #include <unistd.h>
51
52 #include <libcapsicum.h>
53 #include <libcasper.h>
54 #include <libcasper_impl.h>
55 #include <nv.h>
56 #include <pjdlog.h>
57
58 /*
59  * Currently there is only one service_connection per service.
60  * In the future we may want multiple connections from multiple clients
61  * per one service instance, but it has to be carefully designed.
62  * The problem is that we may restrict/sandbox service instance according
63  * to the limits provided. When new connection comes in with different
64  * limits we won't be able to access requested resources.
65  * Not to mention one process will serve to mutiple mutually untrusted
66  * clients and compromise of this service instance by one of its clients
67  * can lead to compromise of the other clients.
68  */
69
70 /*
71  * Client connections to the given service.
72  */
73 #define SERVICE_CONNECTION_MAGIC        0x5e91c0ec
74 struct service_connection {
75         int              sc_magic;
76         cap_channel_t   *sc_chan;
77         nvlist_t        *sc_limits;
78         TAILQ_ENTRY(service_connection) sc_next;
79 };
80
81 #define SERVICE_MAGIC   0x5e91ce
82 struct service {
83         int                      s_magic;
84         char                    *s_name;
85         service_limit_func_t    *s_limit;
86         service_command_func_t  *s_command;
87         TAILQ_HEAD(, service_connection) s_connections;
88 };
89
90 struct service *
91 service_alloc(const char *name, service_limit_func_t *limitfunc,
92     service_command_func_t *commandfunc)
93 {
94         struct service *service;
95
96         service = malloc(sizeof(*service));
97         if (service == NULL)
98                 return (NULL);
99         service->s_name = strdup(name);
100         if (service->s_name == NULL) {
101                 free(service);
102                 return (NULL);
103         }
104         service->s_limit = limitfunc;
105         service->s_command = commandfunc;
106         TAILQ_INIT(&service->s_connections);
107         service->s_magic = SERVICE_MAGIC;
108
109         return (service);
110 }
111
112 void
113 service_free(struct service *service)
114 {
115         struct service_connection *sconn;
116
117         PJDLOG_ASSERT(service->s_magic == SERVICE_MAGIC);
118
119         service->s_magic = 0;
120         while ((sconn = service_connection_first(service)) != NULL)
121                 service_connection_remove(service, sconn);
122         free(service->s_name);
123         free(service);
124 }
125
126 struct service_connection *
127 service_connection_add(struct service *service, int sock,
128     const nvlist_t *limits)
129 {
130         struct service_connection *sconn;
131         int serrno;
132
133         PJDLOG_ASSERT(service->s_magic == SERVICE_MAGIC);
134
135         sconn = malloc(sizeof(*sconn));
136         if (sconn == NULL) {
137                 pjdlog_error("Unable to allocate memory for service connection.");
138                 return (NULL);
139         }
140         sconn->sc_chan = cap_wrap(sock);
141         if (sconn->sc_chan == NULL) {
142                 serrno = errno;
143                 pjdlog_error("Unable to wrap communication channel.");
144                 free(sconn);
145                 errno = serrno;
146                 return (NULL);
147         }
148         if (limits == NULL) {
149                 sconn->sc_limits = NULL;
150         } else {
151                 sconn->sc_limits = nvlist_clone(limits);
152                 if (sconn->sc_limits == NULL) {
153                         serrno = errno;
154                         pjdlog_error("Unable to clone limits.");
155                         (void)cap_unwrap(sconn->sc_chan);
156                         free(sconn);
157                         errno = serrno;
158                         return (NULL);
159                 }
160         }
161         sconn->sc_magic = SERVICE_CONNECTION_MAGIC;
162         TAILQ_INSERT_TAIL(&service->s_connections, sconn, sc_next);
163         return (sconn);
164 }
165
166 void
167 service_connection_remove(struct service *service,
168     struct service_connection *sconn)
169 {
170
171         PJDLOG_ASSERT(service->s_magic == SERVICE_MAGIC);
172         PJDLOG_ASSERT(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
173
174         TAILQ_REMOVE(&service->s_connections, sconn, sc_next);
175         sconn->sc_magic = 0;
176         nvlist_destroy(sconn->sc_limits);
177         cap_close(sconn->sc_chan);
178         free(sconn);
179 }
180
181 int
182 service_connection_clone(struct service *service,
183     struct service_connection *sconn)
184 {
185         struct service_connection *newsconn;
186         int serrno, sock[2];
187
188         if (socketpair(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, sock) < 0)
189                 return (-1);
190
191         newsconn = service_connection_add(service, sock[0],
192             service_connection_get_limits(sconn));
193         if (newsconn == NULL) {
194                 serrno = errno;
195                 close(sock[0]);
196                 close(sock[1]);
197                 errno = serrno;
198                 return (-1);
199         }
200
201         return (sock[1]);
202 }
203
204 struct service_connection *
205 service_connection_first(struct service *service)
206 {
207         struct service_connection *sconn;
208
209         PJDLOG_ASSERT(service->s_magic == SERVICE_MAGIC);
210
211         sconn = TAILQ_FIRST(&service->s_connections);
212         PJDLOG_ASSERT(sconn == NULL ||
213             sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
214         return (sconn);
215 }
216
217 struct service_connection *
218 service_connection_next(struct service_connection *sconn)
219 {
220
221         PJDLOG_ASSERT(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
222
223         sconn = TAILQ_NEXT(sconn, sc_next);
224         PJDLOG_ASSERT(sconn == NULL ||
225             sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
226         return (sconn);
227 }
228
229 cap_channel_t *
230 service_connection_get_chan(const struct service_connection *sconn)
231 {
232
233         PJDLOG_ASSERT(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
234
235         return (sconn->sc_chan);
236 }
237
238 int
239 service_connection_get_sock(const struct service_connection *sconn)
240 {
241
242         PJDLOG_ASSERT(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
243
244         return (cap_sock(sconn->sc_chan));
245 }
246
247 const nvlist_t *
248 service_connection_get_limits(const struct service_connection *sconn)
249 {
250
251         PJDLOG_ASSERT(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
252
253         return (sconn->sc_limits);
254 }
255
256 void
257 service_connection_set_limits(struct service_connection *sconn,
258     nvlist_t *limits)
259 {
260
261         PJDLOG_ASSERT(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
262
263         nvlist_destroy(sconn->sc_limits);
264         sconn->sc_limits = limits;
265 }
266
267 #if 0
268 static void
269 casper_message_connection(struct service *service, const nvlist_t *nvl)
270 {
271
272         service_connection_add(&service->s_connections,
273             nvlist_get_descriptor(nvl, "sock"));
274 }
275
276 static void
277 casper_message(const cap_channel_t *capcas, struct service *service)
278 {
279         const char *cmd;
280         nvlist_t *nvl;
281
282         nvl = cap_recv_nvlist(capcas);
283         if (nvl == NULL)
284                 pjdlog_exit(1, "Unable to receive message from Casper");
285         cmd = nvlist_get_string(nvl, "cmd");
286         if (strcmp(cmd, "connection") == 0)
287                 casper_message_connection(service, nvl);
288         else
289                 PJDLOG_ABORT("Unknown command from Casper: %s.", cmd);
290 }
291 #endif
292
293 void
294 service_message(struct service *service, struct service_connection *sconn)
295 {
296         nvlist_t *nvlin, *nvlout;
297         const char *cmd;
298         int error;
299
300         nvlin = cap_recv_nvlist(service_connection_get_chan(sconn));
301         if (nvlin == NULL) {
302                 if (errno == ENOTCONN) {
303                         pjdlog_debug(1, "Connection closed by the client.");
304                 } else {
305                         pjdlog_errno(LOG_ERR,
306                             "Unable to receive message from client");
307                 }
308                 service_connection_remove(service, sconn);
309                 return;
310         }
311
312         error = EDOOFUS;
313         nvlout = nvlist_create(0);
314
315         cmd = nvlist_get_string(nvlin, "cmd");
316         pjdlog_debug(1, "Command received from client: %s.", cmd);
317         if (pjdlog_debug_get() >= 2)
318                 nvlist_fdump(nvlin, stderr);
319         if (strcmp(cmd, "limit_set") == 0) {
320                 nvlist_t *nvllim;
321
322                 nvllim = nvlist_take_nvlist(nvlin, "limits");
323                 error = service->s_limit(service_connection_get_limits(sconn),
324                     nvllim);
325                 if (error == 0) {
326                         service_connection_set_limits(sconn, nvllim);
327                         /* Function consumes nvllim. */
328                 } else {
329                         nvlist_destroy(nvllim);
330                 }
331         } else if (strcmp(cmd, "limit_get") == 0) {
332                 const nvlist_t *nvllim;
333
334                 nvllim = service_connection_get_limits(sconn);
335                 if (nvllim != NULL)
336                         nvlist_add_nvlist(nvlout, "limits", nvllim);
337                 else
338                         nvlist_add_null(nvlout, "limits");
339                 error = 0;
340         } else if (strcmp(cmd, "clone") == 0) {
341                 int sock;
342
343                 sock = service_connection_clone(service, sconn);
344                 if (sock == -1) {
345                         error = errno;
346                 } else {
347                         nvlist_add_descriptor(nvlout, "sock", sock);
348                         error = 0;
349                 }
350         } else {
351                 error = service->s_command(cmd,
352                     service_connection_get_limits(sconn), nvlin, nvlout);
353         }
354
355         nvlist_destroy(nvlin);
356         nvlist_add_number(nvlout, "error", (uint64_t)error);
357         pjdlog_debug(1, "Sending reply to client (error=%d).", error);
358         if (pjdlog_debug_get() >= 2)
359                 nvlist_fdump(nvlout, stderr);
360
361         if (cap_send_nvlist(service_connection_get_chan(sconn), nvlout) == -1) {
362                 pjdlog_errno(LOG_ERR, "Unable to send message to client");
363                 service_connection_remove(service, sconn);
364         }
365
366         nvlist_destroy(nvlout);
367 }
368
369 static int
370 fd_add(fd_set *fdsp, int maxfd, int fd)
371 {
372
373         FD_SET(fd, fdsp);
374         return (fd > maxfd ? fd : maxfd);
375 }
376
377 int
378 service_start(const char *name, int sock, service_limit_func_t *limitfunc,
379     service_command_func_t *commandfunc, int argc, char *argv[])
380 {
381         struct service *service;
382         struct service_connection *sconn, *sconntmp;
383         fd_set fds;
384         int maxfd, nfds, serrno;
385
386         assert(argc == 2);
387
388         pjdlog_init(PJDLOG_MODE_STD);
389         pjdlog_debug_set(atoi(argv[1]));
390
391         service = service_alloc(name, limitfunc, commandfunc);
392         if (service == NULL)
393                 return (errno);
394         if (service_connection_add(service, sock, NULL) == NULL) {
395                 serrno = errno;
396                 service_free(service);
397                 return (serrno);
398         }
399
400         for (;;) {
401                 FD_ZERO(&fds);
402                 maxfd = -1;
403                 for (sconn = service_connection_first(service); sconn != NULL;
404                     sconn = service_connection_next(sconn)) {
405                         maxfd = fd_add(&fds, maxfd,
406                             service_connection_get_sock(sconn));
407                 }
408
409                 PJDLOG_ASSERT(maxfd >= 0);
410                 PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE);
411                 nfds = select(maxfd + 1, &fds, NULL, NULL, NULL);
412                 if (nfds < 0) {
413                         if (errno != EINTR)
414                                 pjdlog_errno(LOG_ERR, "select() failed");
415                         continue;
416                 } else if (nfds == 0) {
417                         /* Timeout. */
418                         PJDLOG_ABORT("select() timeout");
419                         continue;
420                 }
421
422                 for (sconn = service_connection_first(service); sconn != NULL;
423                     sconn = sconntmp) {
424                         /*
425                          * Prepare for connection to be removed from the list
426                          * on failure.
427                          */
428                         sconntmp = service_connection_next(sconn);
429                         if (FD_ISSET(service_connection_get_sock(sconn), &fds))
430                                 service_message(service, sconn);
431                 }
432                 if (service_connection_first(service) == NULL) {
433                         /*
434                          * No connections left, exiting.
435                          */
436                         break;
437                 }
438         }
439
440         return (0);
441 }