2 * Copyright (c) 2013-16, Stacey D. Son
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/ctype.h>
33 #include <sys/imgact.h>
34 #include <sys/imgact_binmisc.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
43 #include <machine/atomic.h>
46 * Miscellaneous binary interpreter image activator.
48 * If the given target executable's header matches 'xbe_magic' field in the
49 * 'interpreter_list' then it will use the user-level interpreter specified in
50 * the 'xbe_interpreter' field to execute the binary. The 'xbe_magic' field may
51 * be adjusted to a given offset using the value in the 'xbe_moffset' field
52 * and bits of the header may be masked using the 'xbe_mask' field. The
53 * 'interpreter_list' entries are managed using sysctl(3) as described in the
54 * <sys/imgact_binmisc.h> file.
58 * Node of the interpreter list.
60 typedef struct imgact_binmisc_entry {
61 SLIST_ENTRY(imgact_binmisc_entry) link;
65 uint8_t *ibe_interpreter;
66 ssize_t ibe_interp_offset;
67 uint32_t ibe_interp_argcnt;
68 uint32_t ibe_interp_length;
69 uint32_t ibe_argv0_cnt;
73 } imgact_binmisc_entry_t;
78 #define IBC_ADD 1 /* Add given entry. */
79 #define IBC_REMOVE 2 /* Remove entry for a given name. */
80 #define IBC_DISABLE 3 /* Disable entry for a given name. */
81 #define IBC_ENABLE 4 /* Enable entry for a given name. */
82 #define IBC_LOOKUP 5 /* Lookup and return entry for given name. */
83 #define IBC_LIST 6 /* Get a snapshot of the interpretor list. */
86 * Interpreter string macros.
88 * They all start with '#' followed by a single letter:
90 #define ISM_POUND '#' /* "##" is the escape sequence for single #. */
91 #define ISM_OLD_ARGV0 'a' /* "#a" is replaced with the old argv0. */
93 MALLOC_DEFINE(M_BINMISC, KMOD_NAME, "misc binary image activator");
95 /* The interpreter list. */
96 static SLIST_HEAD(, imgact_binmisc_entry) interpreter_list =
97 SLIST_HEAD_INITIALIZER(interpreter_list);
99 static int interp_list_entry_count;
101 static struct sx interp_list_sx;
103 #define INTERP_LIST_WLOCK() sx_xlock(&interp_list_sx)
104 #define INTERP_LIST_RLOCK() sx_slock(&interp_list_sx)
105 #define INTERP_LIST_WUNLOCK() sx_xunlock(&interp_list_sx)
106 #define INTERP_LIST_RUNLOCK() sx_sunlock(&interp_list_sx)
108 #define INTERP_LIST_LOCK_INIT() sx_init(&interp_list_sx, KMOD_NAME)
109 #define INTERP_LIST_LOCK_DESTROY() sx_destroy(&interp_list_sx)
111 #define INTERP_LIST_ASSERT_LOCKED() sx_assert(&interp_list_sx, SA_LOCKED)
114 * Populate the entry with the information about the interpreter.
117 imgact_binmisc_populate_interp(char *str, imgact_binmisc_entry_t *ibe)
119 uint32_t len = 0, argc = 1;
120 char t[IBE_INTERP_LEN_MAX];
123 memset(t, 0, sizeof(t));
126 * Normalize interpreter string. Replace white space between args with
130 while (*sp != '\0') {
131 if (*sp == ' ' || *sp == '\t') {
132 if (++len >= IBE_INTERP_LEN_MAX)
136 while (*sp == ' ' || *sp == '\t')
147 ibe->ibe_interpreter = malloc(len, M_BINMISC, M_WAITOK|M_ZERO);
149 /* Populate all the ibe fields for the interpreter. */
150 memcpy(ibe->ibe_interpreter, t, len);
151 ibe->ibe_interp_argcnt = argc;
152 ibe->ibe_interp_length = len;
156 * Allocate memory and populate a new entry for the interpreter table.
158 static imgact_binmisc_entry_t *
159 imgact_binmisc_new_entry(ximgact_binmisc_entry_t *xbe, ssize_t interp_offset,
162 imgact_binmisc_entry_t *ibe = NULL;
163 size_t namesz = min(strlen(xbe->xbe_name) + 1, IBE_NAME_MAX);
165 ibe = malloc(sizeof(*ibe), M_BINMISC, M_WAITOK|M_ZERO);
167 ibe->ibe_name = malloc(namesz, M_BINMISC, M_WAITOK|M_ZERO);
168 strlcpy(ibe->ibe_name, xbe->xbe_name, namesz);
170 imgact_binmisc_populate_interp(xbe->xbe_interpreter, ibe);
172 ibe->ibe_magic = malloc(xbe->xbe_msize, M_BINMISC, M_WAITOK|M_ZERO);
173 memcpy(ibe->ibe_magic, xbe->xbe_magic, xbe->xbe_msize);
175 ibe->ibe_mask = malloc(xbe->xbe_msize, M_BINMISC, M_WAITOK|M_ZERO);
176 memcpy(ibe->ibe_mask, xbe->xbe_mask, xbe->xbe_msize);
178 ibe->ibe_moffset = xbe->xbe_moffset;
179 ibe->ibe_msize = xbe->xbe_msize;
180 ibe->ibe_flags = xbe->xbe_flags;
181 ibe->ibe_interp_offset = interp_offset;
182 ibe->ibe_argv0_cnt = argv0_cnt;
187 * Free the allocated memory for a given list item.
190 imgact_binmisc_destroy_entry(imgact_binmisc_entry_t *ibe)
195 free(ibe->ibe_magic, M_BINMISC);
197 free(ibe->ibe_mask, M_BINMISC);
198 if (ibe->ibe_interpreter)
199 free(ibe->ibe_interpreter, M_BINMISC);
201 free(ibe->ibe_name, M_BINMISC);
203 free(ibe, M_BINMISC);
207 * Find the interpreter in the list by the given name. Return NULL if not
210 static imgact_binmisc_entry_t *
211 imgact_binmisc_find_entry(char *name)
213 imgact_binmisc_entry_t *ibe;
215 INTERP_LIST_ASSERT_LOCKED();
217 SLIST_FOREACH(ibe, &interpreter_list, link) {
218 if (strncmp(name, ibe->ibe_name, IBE_NAME_MAX) == 0)
226 * Add the given interpreter if it doesn't already exist. Return EEXIST
227 * if the name already exist in the interpreter list.
230 imgact_binmisc_add_entry(ximgact_binmisc_entry_t *xbe)
232 imgact_binmisc_entry_t *ibe;
234 ssize_t interp_offset;
237 if (xbe->xbe_msize > IBE_MAGIC_MAX)
240 for(cnt = 0, p = xbe->xbe_name; *p != 0; cnt++, p++)
241 if (cnt >= IBE_NAME_MAX || !isascii((int)*p))
244 for(cnt = 0, p = xbe->xbe_interpreter; *p != 0; cnt++, p++)
245 if (cnt >= IBE_INTERP_LEN_MAX || !isascii((int)*p))
248 /* Make sure we don't have any invalid #'s. */
249 p = xbe->xbe_interpreter;
252 while ((p = strchr(p, '#')) != NULL) {
267 /* Anything besides the above is invalid. */
273 if (imgact_binmisc_find_entry(xbe->xbe_name) != NULL) {
274 INTERP_LIST_WUNLOCK();
278 /* Preallocate a new entry. */
279 ibe = imgact_binmisc_new_entry(xbe, interp_offset, argv0_cnt);
281 SLIST_INSERT_HEAD(&interpreter_list, ibe, link);
282 interp_list_entry_count++;
283 INTERP_LIST_WUNLOCK();
289 * Remove the interpreter in the list with the given name. Return ENOENT
293 imgact_binmisc_remove_entry(char *name)
295 imgact_binmisc_entry_t *ibe;
298 if ((ibe = imgact_binmisc_find_entry(name)) == NULL) {
299 INTERP_LIST_WUNLOCK();
302 SLIST_REMOVE(&interpreter_list, ibe, imgact_binmisc_entry, link);
303 interp_list_entry_count--;
304 INTERP_LIST_WUNLOCK();
306 imgact_binmisc_destroy_entry(ibe);
312 * Disable the interpreter in the list with the given name. Return ENOENT
316 imgact_binmisc_disable_entry(char *name)
318 imgact_binmisc_entry_t *ibe;
321 if ((ibe = imgact_binmisc_find_entry(name)) == NULL) {
322 INTERP_LIST_WUNLOCK();
326 ibe->ibe_flags &= ~IBF_ENABLED;
327 INTERP_LIST_WUNLOCK();
333 * Enable the interpreter in the list with the given name. Return ENOENT
337 imgact_binmisc_enable_entry(char *name)
339 imgact_binmisc_entry_t *ibe;
342 if ((ibe = imgact_binmisc_find_entry(name)) == NULL) {
343 INTERP_LIST_WUNLOCK();
347 ibe->ibe_flags |= IBF_ENABLED;
348 INTERP_LIST_WUNLOCK();
354 imgact_binmisc_populate_xbe(ximgact_binmisc_entry_t *xbe,
355 imgact_binmisc_entry_t *ibe)
359 INTERP_LIST_ASSERT_LOCKED();
360 memset(xbe, 0, sizeof(*xbe));
361 strlcpy(xbe->xbe_name, ibe->ibe_name, IBE_NAME_MAX);
363 /* Copy interpreter string. Replace NULL breaks with space. */
364 memcpy(xbe->xbe_interpreter, ibe->ibe_interpreter,
365 ibe->ibe_interp_length);
366 for(i = 0; i < (ibe->ibe_interp_length - 1); i++)
367 if (xbe->xbe_interpreter[i] == '\0')
368 xbe->xbe_interpreter[i] = ' ';
370 memcpy(xbe->xbe_magic, ibe->ibe_magic, ibe->ibe_msize);
371 memcpy(xbe->xbe_mask, ibe->ibe_mask, ibe->ibe_msize);
372 xbe->xbe_version = IBE_VERSION;
373 xbe->xbe_flags = ibe->ibe_flags;
374 xbe->xbe_moffset = ibe->ibe_moffset;
375 xbe->xbe_msize = ibe->ibe_msize;
381 * Retrieve the interpreter with the give name and populate the
382 * ximgact_binmisc_entry structure. Return ENOENT if not found.
385 imgact_binmisc_lookup_entry(char *name, ximgact_binmisc_entry_t *xbe)
387 imgact_binmisc_entry_t *ibe;
391 if ((ibe = imgact_binmisc_find_entry(name)) == NULL) {
392 INTERP_LIST_RUNLOCK();
396 error = imgact_binmisc_populate_xbe(xbe, ibe);
397 INTERP_LIST_RUNLOCK();
403 * Get a snapshot of all the interpreter entries in the list.
406 imgact_binmisc_get_all_entries(struct sysctl_req *req)
408 ximgact_binmisc_entry_t *xbe, *xbep;
409 imgact_binmisc_entry_t *ibe;
410 int error = 0, count;
413 count = interp_list_entry_count;
414 xbe = malloc(sizeof(*xbe) * count, M_BINMISC, M_WAITOK|M_ZERO);
417 SLIST_FOREACH(ibe, &interpreter_list, link) {
418 error = imgact_binmisc_populate_xbe(xbep++, ibe);
422 INTERP_LIST_RUNLOCK();
425 error = SYSCTL_OUT(req, xbe, sizeof(*xbe) * count);
427 free(xbe, M_BINMISC);
432 * sysctl() handler for munipulating interpretor table.
433 * Not MP safe (locked by sysctl).
436 sysctl_kern_binmisc(SYSCTL_HANDLER_ARGS)
438 ximgact_binmisc_entry_t xbe;
443 /* Add an entry. Limited to IBE_MAX_ENTRIES. */
444 error = SYSCTL_IN(req, &xbe, sizeof(xbe));
447 if (IBE_VERSION != xbe.xbe_version)
449 if ((xbe.xbe_flags & ~IBF_VALID_UFLAGS) != 0)
451 if (interp_list_entry_count == IBE_MAX_ENTRIES)
453 error = imgact_binmisc_add_entry(&xbe);
457 /* Remove an entry. */
458 error = SYSCTL_IN(req, &xbe, sizeof(xbe));
461 if (IBE_VERSION != xbe.xbe_version)
463 error = imgact_binmisc_remove_entry(xbe.xbe_name);
467 /* Disable an entry. */
468 error = SYSCTL_IN(req, &xbe, sizeof(xbe));
471 if (IBE_VERSION != xbe.xbe_version)
473 error = imgact_binmisc_disable_entry(xbe.xbe_name);
477 /* Enable an entry. */
478 error = SYSCTL_IN(req, &xbe, sizeof(xbe));
481 if (IBE_VERSION != xbe.xbe_version)
483 error = imgact_binmisc_enable_entry(xbe.xbe_name);
487 /* Lookup an entry. */
488 error = SYSCTL_IN(req, &xbe, sizeof(xbe));
491 if (IBE_VERSION != xbe.xbe_version)
493 error = imgact_binmisc_lookup_entry(xbe.xbe_name, &xbe);
495 error = SYSCTL_OUT(req, &xbe, sizeof(xbe));
499 /* Return a snapshot of the interpretor list. */
502 /* No pointer then just return the list size. */
503 error = SYSCTL_OUT(req, 0, interp_list_entry_count *
504 sizeof(ximgact_binmisc_entry_t));
510 error = imgact_binmisc_get_all_entries(req);
520 SYSCTL_NODE(_kern, OID_AUTO, binmisc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
521 "Image activator for miscellaneous binaries");
523 SYSCTL_PROC(_kern_binmisc, OID_AUTO, add,
524 CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_ADD,
525 sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
526 "Add an activator entry");
528 SYSCTL_PROC(_kern_binmisc, OID_AUTO, remove,
529 CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_REMOVE,
530 sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
531 "Remove an activator entry");
533 SYSCTL_PROC(_kern_binmisc, OID_AUTO, disable,
534 CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_DISABLE,
535 sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
536 "Disable an activator entry");
538 SYSCTL_PROC(_kern_binmisc, OID_AUTO, enable,
539 CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_ENABLE,
540 sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
541 "Enable an activator entry");
543 SYSCTL_PROC(_kern_binmisc, OID_AUTO, lookup,
544 CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_RW|CTLFLAG_ANYBODY, NULL, IBC_LOOKUP,
545 sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
546 "Lookup an activator entry");
548 SYSCTL_PROC(_kern_binmisc, OID_AUTO, list,
549 CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_ANYBODY, NULL, IBC_LIST,
550 sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
551 "Get snapshot of all the activator entries");
553 static imgact_binmisc_entry_t *
554 imgact_binmisc_find_interpreter(const char *image_header)
556 imgact_binmisc_entry_t *ibe;
561 INTERP_LIST_ASSERT_LOCKED();
563 SLIST_FOREACH(ibe, &interpreter_list, link) {
564 if (!(IBF_ENABLED & ibe->ibe_flags))
567 p = image_header + ibe->ibe_moffset;
569 if (IBF_USE_MASK & ibe->ibe_flags) {
570 /* Compare using mask. */
571 for (i = 0; i < sz; i++)
572 if ((*p++ ^ ibe->ibe_magic[i]) &
576 for (i = 0; i < sz; i++)
577 if (*p++ ^ ibe->ibe_magic[i])
580 if (i == ibe->ibe_msize)
587 imgact_binmisc_exec(struct image_params *imgp)
589 const char *image_header = imgp->image_header;
590 const char *fname = NULL;
595 size_t namelen, offset;
596 imgact_binmisc_entry_t *ibe;
602 /* Do we have an interpreter for the given image header? */
604 if ((ibe = imgact_binmisc_find_interpreter(image_header)) == NULL) {
609 /* No interpreter nesting allowed. */
610 if (imgp->interpreted & IMGACT_BINMISC) {
615 imgp->interpreted |= IMGACT_BINMISC;
618 * Don't bother with the overhead of putting fname together if we're not
621 if (ibe->ibe_argv0_cnt != 0) {
622 if (imgp->args->fname != NULL) {
623 fname = imgp->args->fname;
625 /* Use the fdescfs(5) path for fexecve(2). */
626 sname = sbuf_new_auto();
627 sbuf_printf(sname, "/dev/fd/%d", imgp->args->fd);
629 fname = sbuf_data(sname);
632 namelen = strlen(fname);
636 * We need to "push" the interpreter in the arg[] list. To do this,
637 * we first shift all the other values in the `begin_argv' area to
638 * provide the exact amount of room for the values added. Set up
639 * `offset' as the number of bytes to be added to the `begin_argv'
640 * area. ibe_interp_offset is the fixed offset from macros present in
641 * the interpreter string.
643 offset = ibe->ibe_interp_length + ibe->ibe_interp_offset;
645 /* Variable offset to be added from macros to the interpreter string. */
646 MPASS(ibe->ibe_argv0_cnt == 0 || namelen > 0);
647 offset += ibe->ibe_argv0_cnt * (namelen - 2);
649 /* Make room for the interpreter */
650 error = exec_args_adjust_args(imgp->args, 0, offset);
655 /* Add the new argument(s) in the count. */
656 imgp->args->argc += ibe->ibe_interp_argcnt;
659 * The original arg[] list has been shifted appropriately. Copy in
660 * the interpreter path.
662 s = ibe->ibe_interpreter;
663 d = imgp->args->begin_argv;
667 /* Handle "#" in interpreter string. */
671 /* "##": Replace with a single '#' */
675 /* "#a": Replace with old arg0 (fname). */
676 MPASS(ibe->ibe_argv0_cnt >= ++argv0_cnt);
677 memcpy(d, fname, namelen);
681 __assert_unreachable();
685 /* Replace space with NUL to separate arguments. */
696 /* Catch ibe->ibe_argv0_cnt counting more #a than we did. */
697 MPASS(ibe->ibe_argv0_cnt == argv0_cnt);
698 imgp->interpreter_name = imgp->args->begin_argv;
701 INTERP_LIST_RUNLOCK();
708 imgact_binmisc_init(void *arg)
711 INTERP_LIST_LOCK_INIT();
715 imgact_binmisc_fini(void *arg)
717 imgact_binmisc_entry_t *ibe, *ibe_tmp;
719 /* Free all the interpreters. */
721 SLIST_FOREACH_SAFE(ibe, &interpreter_list, link, ibe_tmp) {
722 SLIST_REMOVE(&interpreter_list, ibe, imgact_binmisc_entry,
724 imgact_binmisc_destroy_entry(ibe);
726 INTERP_LIST_WUNLOCK();
728 INTERP_LIST_LOCK_DESTROY();
731 SYSINIT(imgact_binmisc, SI_SUB_EXEC, SI_ORDER_MIDDLE, imgact_binmisc_init,
733 SYSUNINIT(imgact_binmisc, SI_SUB_EXEC, SI_ORDER_MIDDLE, imgact_binmisc_fini,
737 * Tell kern_execve.c about it, with a little help from the linker.
739 static struct execsw imgact_binmisc_execsw = {
740 .ex_imgact = imgact_binmisc_exec,
743 EXEC_SET(imgact_binmisc, imgact_binmisc_execsw);