2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 The FreeBSD Foundation
5 * Copyright (c) 2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
54 #include "libcasper.h"
55 #include "libcasper_impl.h"
58 * Currently there is only one service_connection per service.
59 * In the future we may want multiple connections from multiple clients
60 * per one service instance, but it has to be carefully designed.
61 * The problem is that we may restrict/sandbox service instance according
62 * to the limits provided. When new connection comes in with different
63 * limits we won't be able to access requested resources.
64 * Not to mention one process will serve to mutiple mutually untrusted
65 * clients and compromise of this service instance by one of its clients
66 * can lead to compromise of the other clients.
70 * Client connections to the given service.
72 #define SERVICE_CONNECTION_MAGIC 0x5e91c0ec
73 struct service_connection {
75 cap_channel_t *sc_chan;
77 TAILQ_ENTRY(service_connection) sc_next;
80 #define SERVICE_MAGIC 0x5e91ce
85 service_limit_func_t *s_limit;
86 service_command_func_t *s_command;
87 TAILQ_HEAD(, service_connection) s_connections;
91 service_alloc(const char *name, service_limit_func_t *limitfunc,
92 service_command_func_t *commandfunc, uint64_t flags)
94 struct service *service;
96 service = malloc(sizeof(*service));
99 service->s_name = strdup(name);
100 if (service->s_name == NULL) {
104 service->s_limit = limitfunc;
105 service->s_command = commandfunc;
106 service->s_flags = flags;
107 TAILQ_INIT(&service->s_connections);
108 service->s_magic = SERVICE_MAGIC;
114 service_free(struct service *service)
116 struct service_connection *sconn;
118 assert(service->s_magic == SERVICE_MAGIC);
120 service->s_magic = 0;
121 while ((sconn = service_connection_first(service)) != NULL)
122 service_connection_remove(service, sconn);
123 free(service->s_name);
127 struct service_connection *
128 service_connection_add(struct service *service, int sock,
129 const nvlist_t *limits)
131 struct service_connection *sconn;
134 assert(service->s_magic == SERVICE_MAGIC);
136 sconn = malloc(sizeof(*sconn));
139 sconn->sc_chan = cap_wrap(sock);
140 if (sconn->sc_chan == NULL) {
146 if (limits == NULL) {
147 sconn->sc_limits = NULL;
149 sconn->sc_limits = nvlist_clone(limits);
150 if (sconn->sc_limits == NULL) {
152 (void)cap_unwrap(sconn->sc_chan);
158 sconn->sc_magic = SERVICE_CONNECTION_MAGIC;
159 TAILQ_INSERT_TAIL(&service->s_connections, sconn, sc_next);
164 service_connection_remove(struct service *service,
165 struct service_connection *sconn)
168 assert(service->s_magic == SERVICE_MAGIC);
169 assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
171 TAILQ_REMOVE(&service->s_connections, sconn, sc_next);
173 nvlist_destroy(sconn->sc_limits);
174 cap_close(sconn->sc_chan);
179 service_connection_clone(struct service *service,
180 struct service_connection *sconn)
182 struct service_connection *newsconn;
185 if (socketpair(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, sock) < 0)
188 newsconn = service_connection_add(service, sock[0],
189 service_connection_get_limits(sconn));
190 if (newsconn == NULL) {
201 struct service_connection *
202 service_connection_first(struct service *service)
204 struct service_connection *sconn;
206 assert(service->s_magic == SERVICE_MAGIC);
208 sconn = TAILQ_FIRST(&service->s_connections);
209 assert(sconn == NULL ||
210 sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
214 struct service_connection *
215 service_connection_next(struct service_connection *sconn)
218 assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
220 sconn = TAILQ_NEXT(sconn, sc_next);
221 assert(sconn == NULL ||
222 sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
227 service_connection_get_chan(const struct service_connection *sconn)
230 assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
232 return (sconn->sc_chan);
236 service_connection_get_sock(const struct service_connection *sconn)
239 assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
241 return (cap_sock(sconn->sc_chan));
245 service_connection_get_limits(const struct service_connection *sconn)
248 assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
250 return (sconn->sc_limits);
254 service_connection_set_limits(struct service_connection *sconn,
258 assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
260 nvlist_destroy(sconn->sc_limits);
261 sconn->sc_limits = limits;
265 service_message(struct service *service, struct service_connection *sconn)
267 nvlist_t *nvlin, *nvlout;
271 nvlin = cap_recv_nvlist(service_connection_get_chan(sconn), 0);
273 service_connection_remove(service, sconn);
278 nvlout = nvlist_create(0);
280 cmd = nvlist_get_string(nvlin, "cmd");
281 if (strcmp(cmd, "limit_set") == 0) {
284 nvllim = nvlist_take_nvlist(nvlin, "limits");
285 if (service->s_limit == NULL) {
288 error = service->s_limit(
289 service_connection_get_limits(sconn), nvllim);
292 service_connection_set_limits(sconn, nvllim);
293 /* Function consumes nvllim. */
295 nvlist_destroy(nvllim);
297 } else if (strcmp(cmd, "limit_get") == 0) {
298 const nvlist_t *nvllim;
300 nvllim = service_connection_get_limits(sconn);
302 nvlist_add_nvlist(nvlout, "limits", nvllim);
304 nvlist_add_null(nvlout, "limits");
306 } else if (strcmp(cmd, "clone") == 0) {
309 sock = service_connection_clone(service, sconn);
313 nvlist_move_descriptor(nvlout, "sock", sock);
317 error = service->s_command(cmd,
318 service_connection_get_limits(sconn), nvlin, nvlout);
321 nvlist_destroy(nvlin);
322 nvlist_add_number(nvlout, "error", (uint64_t)error);
324 if (cap_send_nvlist(service_connection_get_chan(sconn), nvlout) == -1)
325 service_connection_remove(service, sconn);
327 nvlist_destroy(nvlout);
331 fd_add(fd_set *fdsp, int maxfd, int fd)
335 return (fd > maxfd ? fd : maxfd);
339 service_name(struct service *service)
342 assert(service->s_magic == SERVICE_MAGIC);
343 return (service->s_name);
351 fd = open(_PATH_DEVNULL, O_RDWR);
353 errx(1, "Unable to open %s", _PATH_DEVNULL);
356 errx(1, "Unable to detach from session");
358 if (dup2(fd, STDIN_FILENO) == -1)
359 errx(1, "Unable to cover stdin");
360 if (dup2(fd, STDOUT_FILENO) == -1)
361 errx(1, "Unable to cover stdout");
362 if (dup2(fd, STDERR_FILENO) == -1)
363 errx(1, "Unable to cover stderr");
365 if (fd > STDERR_FILENO)
370 service_clean(int sock, int procfd, uint64_t flags)
372 int fd, maxfd, minfd;
374 assert(sock > STDERR_FILENO);
375 assert(procfd > STDERR_FILENO);
376 assert(sock != procfd);
378 if ((flags & CASPER_SERVICE_STDIO) == 0)
381 if ((flags & CASPER_SERVICE_FD) == 0) {
390 for (fd = STDERR_FILENO + 1; fd < maxfd; fd++) {
394 closefrom(maxfd + 1);
399 service_start(struct service *service, int sock, int procfd)
401 struct service_connection *sconn, *sconntmp;
405 assert(service != NULL);
406 assert(service->s_magic == SERVICE_MAGIC);
407 setproctitle("%s", service->s_name);
408 service_clean(sock, procfd, service->s_flags);
410 if (service_connection_add(service, sock, NULL) == NULL)
416 for (sconn = service_connection_first(service); sconn != NULL;
417 sconn = service_connection_next(sconn)) {
418 maxfd = fd_add(&fds, maxfd,
419 service_connection_get_sock(sconn));
423 assert(maxfd + 1 <= (int)FD_SETSIZE);
424 nfds = select(maxfd + 1, &fds, NULL, NULL, NULL);
429 } else if (nfds == 0) {
434 for (sconn = service_connection_first(service); sconn != NULL;
437 * Prepare for connection to be removed from the list
440 sconntmp = service_connection_next(sconn);
441 if (FD_ISSET(service_connection_get_sock(sconn), &fds))
442 service_message(service, sconn);
444 if (service_connection_first(service) == NULL) {
446 * No connections left, exiting.