]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libcasper/libcasper/service.c
Add SPDX tags for libcasper(3) and services.
[FreeBSD/FreeBSD.git] / lib / libcasper / libcasper / service.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 The FreeBSD Foundation
5  * Copyright (c) 2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
6  * All rights reserved.
7  *
8  * This software was developed by Pawel Jakub Dawidek under sponsorship from
9  * the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/types.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
39 #include <sys/nv.h>
40
41 #include <assert.h>
42 #include <dirent.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <paths.h>
47 #include <stdbool.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <strings.h>
52 #include <unistd.h>
53
54 #include "libcasper.h"
55 #include "libcasper_impl.h"
56
57 /*
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.
67  */
68
69 /*
70  * Client connections to the given service.
71  */
72 #define SERVICE_CONNECTION_MAGIC        0x5e91c0ec
73 struct service_connection {
74         int              sc_magic;
75         cap_channel_t   *sc_chan;
76         nvlist_t        *sc_limits;
77         TAILQ_ENTRY(service_connection) sc_next;
78 };
79
80 #define SERVICE_MAGIC   0x5e91ce
81 struct service {
82         int                      s_magic;
83         char                    *s_name;
84         uint64_t                 s_flags;
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, uint64_t flags)
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         service->s_flags = flags;
107         TAILQ_INIT(&service->s_connections);
108         service->s_magic = SERVICE_MAGIC;
109
110         return (service);
111 }
112
113 void
114 service_free(struct service *service)
115 {
116         struct service_connection *sconn;
117
118         assert(service->s_magic == SERVICE_MAGIC);
119
120         service->s_magic = 0;
121         while ((sconn = service_connection_first(service)) != NULL)
122                 service_connection_remove(service, sconn);
123         free(service->s_name);
124         free(service);
125 }
126
127 struct service_connection *
128 service_connection_add(struct service *service, int sock,
129     const nvlist_t *limits)
130 {
131         struct service_connection *sconn;
132         int serrno;
133
134         assert(service->s_magic == SERVICE_MAGIC);
135
136         sconn = malloc(sizeof(*sconn));
137         if (sconn == NULL)
138                 return (NULL);
139         sconn->sc_chan = cap_wrap(sock);
140         if (sconn->sc_chan == NULL) {
141                 serrno = errno;
142                 free(sconn);
143                 errno = serrno;
144                 return (NULL);
145         }
146         if (limits == NULL) {
147                 sconn->sc_limits = NULL;
148         } else {
149                 sconn->sc_limits = nvlist_clone(limits);
150                 if (sconn->sc_limits == NULL) {
151                         serrno = errno;
152                         (void)cap_unwrap(sconn->sc_chan);
153                         free(sconn);
154                         errno = serrno;
155                         return (NULL);
156                 }
157         }
158         sconn->sc_magic = SERVICE_CONNECTION_MAGIC;
159         TAILQ_INSERT_TAIL(&service->s_connections, sconn, sc_next);
160         return (sconn);
161 }
162
163 void
164 service_connection_remove(struct service *service,
165     struct service_connection *sconn)
166 {
167
168         assert(service->s_magic == SERVICE_MAGIC);
169         assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
170
171         TAILQ_REMOVE(&service->s_connections, sconn, sc_next);
172         sconn->sc_magic = 0;
173         nvlist_destroy(sconn->sc_limits);
174         cap_close(sconn->sc_chan);
175         free(sconn);
176 }
177
178 int
179 service_connection_clone(struct service *service,
180     struct service_connection *sconn)
181 {
182         struct service_connection *newsconn;
183         int serrno, sock[2];
184
185         if (socketpair(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, sock) < 0)
186                 return (-1);
187
188         newsconn = service_connection_add(service, sock[0],
189             service_connection_get_limits(sconn));
190         if (newsconn == NULL) {
191                 serrno = errno;
192                 close(sock[0]);
193                 close(sock[1]);
194                 errno = serrno;
195                 return (-1);
196         }
197
198         return (sock[1]);
199 }
200
201 struct service_connection *
202 service_connection_first(struct service *service)
203 {
204         struct service_connection *sconn;
205
206         assert(service->s_magic == SERVICE_MAGIC);
207
208         sconn = TAILQ_FIRST(&service->s_connections);
209         assert(sconn == NULL ||
210             sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
211         return (sconn);
212 }
213
214 struct service_connection *
215 service_connection_next(struct service_connection *sconn)
216 {
217
218         assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
219
220         sconn = TAILQ_NEXT(sconn, sc_next);
221         assert(sconn == NULL ||
222             sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
223         return (sconn);
224 }
225
226 cap_channel_t *
227 service_connection_get_chan(const struct service_connection *sconn)
228 {
229
230         assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
231
232         return (sconn->sc_chan);
233 }
234
235 int
236 service_connection_get_sock(const struct service_connection *sconn)
237 {
238
239         assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
240
241         return (cap_sock(sconn->sc_chan));
242 }
243
244 const nvlist_t *
245 service_connection_get_limits(const struct service_connection *sconn)
246 {
247
248         assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
249
250         return (sconn->sc_limits);
251 }
252
253 void
254 service_connection_set_limits(struct service_connection *sconn,
255     nvlist_t *limits)
256 {
257
258         assert(sconn->sc_magic == SERVICE_CONNECTION_MAGIC);
259
260         nvlist_destroy(sconn->sc_limits);
261         sconn->sc_limits = limits;
262 }
263
264 void
265 service_message(struct service *service, struct service_connection *sconn)
266 {
267         nvlist_t *nvlin, *nvlout;
268         const char *cmd;
269         int error;
270
271         nvlin = cap_recv_nvlist(service_connection_get_chan(sconn), 0);
272         if (nvlin == NULL) {
273                 service_connection_remove(service, sconn);
274                 return;
275         }
276
277         error = EDOOFUS;
278         nvlout = nvlist_create(0);
279
280         cmd = nvlist_get_string(nvlin, "cmd");
281         if (strcmp(cmd, "limit_set") == 0) {
282                 nvlist_t *nvllim;
283
284                 nvllim = nvlist_take_nvlist(nvlin, "limits");
285                 if (service->s_limit == NULL) {
286                         error = EOPNOTSUPP;
287                 } else {
288                         error = service->s_limit(
289                             service_connection_get_limits(sconn), nvllim);
290                 }
291                 if (error == 0) {
292                         service_connection_set_limits(sconn, nvllim);
293                         /* Function consumes nvllim. */
294                 } else {
295                         nvlist_destroy(nvllim);
296                 }
297         } else if (strcmp(cmd, "limit_get") == 0) {
298                 const nvlist_t *nvllim;
299
300                 nvllim = service_connection_get_limits(sconn);
301                 if (nvllim != NULL)
302                         nvlist_add_nvlist(nvlout, "limits", nvllim);
303                 else
304                         nvlist_add_null(nvlout, "limits");
305                 error = 0;
306         } else if (strcmp(cmd, "clone") == 0) {
307                 int sock;
308
309                 sock = service_connection_clone(service, sconn);
310                 if (sock == -1) {
311                         error = errno;
312                 } else {
313                         nvlist_move_descriptor(nvlout, "sock", sock);
314                         error = 0;
315                 }
316         } else {
317                 error = service->s_command(cmd,
318                     service_connection_get_limits(sconn), nvlin, nvlout);
319         }
320
321         nvlist_destroy(nvlin);
322         nvlist_add_number(nvlout, "error", (uint64_t)error);
323
324         if (cap_send_nvlist(service_connection_get_chan(sconn), nvlout) == -1)
325                 service_connection_remove(service, sconn);
326
327         nvlist_destroy(nvlout);
328 }
329
330 static int
331 fd_add(fd_set *fdsp, int maxfd, int fd)
332 {
333
334         FD_SET(fd, fdsp);
335         return (fd > maxfd ? fd : maxfd);
336 }
337
338 const char *
339 service_name(struct service *service)
340 {
341
342         assert(service->s_magic == SERVICE_MAGIC);
343         return (service->s_name);
344 }
345
346 static void
347 stdnull(void)
348 {
349         int fd;
350
351         fd = open(_PATH_DEVNULL, O_RDWR);
352         if (fd == -1)
353                 errx(1, "Unable to open %s", _PATH_DEVNULL);
354
355         if (setsid() == -1)
356                 errx(1, "Unable to detach from session");
357
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");
364
365         if (fd > STDERR_FILENO)
366                 close(fd);
367 }
368
369 static void
370 service_clean(int sock, int procfd, uint64_t flags)
371 {
372         int fd, maxfd, minfd;
373
374         assert(sock > STDERR_FILENO);
375         assert(procfd > STDERR_FILENO);
376         assert(sock != procfd);
377
378         if ((flags & CASPER_SERVICE_STDIO) == 0)
379                 stdnull();
380
381         if ((flags & CASPER_SERVICE_FD) == 0) {
382                 if (procfd > sock) {
383                         maxfd = procfd;
384                         minfd = sock;
385                 } else {
386                         maxfd = sock;
387                         minfd = procfd;
388                 }
389
390                 for (fd = STDERR_FILENO + 1; fd < maxfd; fd++) {
391                         if (fd != minfd)
392                                 close(fd);
393                 }
394                 closefrom(maxfd + 1);
395         }
396 }
397
398 void
399 service_start(struct service *service, int sock, int procfd)
400 {
401         struct service_connection *sconn, *sconntmp;
402         fd_set fds;
403         int maxfd, nfds;
404
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);
409
410         if (service_connection_add(service, sock, NULL) == NULL)
411                 exit(1);
412
413         for (;;) {
414                 FD_ZERO(&fds);
415                 maxfd = -1;
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));
420                 }
421
422                 assert(maxfd >= 0);
423                 assert(maxfd + 1 <= (int)FD_SETSIZE);
424                 nfds = select(maxfd + 1, &fds, NULL, NULL, NULL);
425                 if (nfds < 0) {
426                         if (errno != EINTR)
427                                 exit(1);
428                         continue;
429                 } else if (nfds == 0) {
430                         /* Timeout. */
431                         abort();
432                 }
433
434                 for (sconn = service_connection_first(service); sconn != NULL;
435                     sconn = sconntmp) {
436                         /*
437                          * Prepare for connection to be removed from the list
438                          * on failure.
439                          */
440                         sconntmp = service_connection_next(sconn);
441                         if (FD_ISSET(service_connection_get_sock(sconn), &fds))
442                                 service_message(service, sconn);
443                 }
444                 if (service_connection_first(service) == NULL) {
445                         /*
446                          * No connections left, exiting.
447                          */
448                         break;
449                 }
450         }
451
452         exit(0);
453 }