xref: /freebsd/sys/kern/imgact_binmisc.c (revision 4e83b32a80574fef36c0cf23694d97f1854b1f7e)
16d756449SSean Bruno /*-
25f98711dSSean Bruno  * Copyright (c) 2013-15, Stacey D. Son
36d756449SSean Bruno  * All rights reserved.
46d756449SSean Bruno  *
56d756449SSean Bruno  * Redistribution and use in source and binary forms, with or without
66d756449SSean Bruno  * modification, are permitted provided that the following conditions
76d756449SSean Bruno  * are met:
86d756449SSean Bruno  * 1. Redistributions of source code must retain the above copyright
96d756449SSean Bruno  *    notice, this list of conditions and the following disclaimer.
106d756449SSean Bruno  * 2. Redistributions in binary form must reproduce the above copyright
116d756449SSean Bruno  *    notice, this list of conditions and the following disclaimer in the
126d756449SSean Bruno  *    documentation and/or other materials provided with the distribution.
136d756449SSean Bruno  *
146d756449SSean Bruno  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156d756449SSean Bruno  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166d756449SSean Bruno  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176d756449SSean Bruno  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
186d756449SSean Bruno  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196d756449SSean Bruno  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206d756449SSean Bruno  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216d756449SSean Bruno  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226d756449SSean Bruno  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236d756449SSean Bruno  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246d756449SSean Bruno  * SUCH DAMAGE.
256d756449SSean Bruno  */
266d756449SSean Bruno 
276d756449SSean Bruno #include <sys/cdefs.h>
286d756449SSean Bruno __FBSDID("$FreeBSD$");
296d756449SSean Bruno 
306d756449SSean Bruno #include <sys/param.h>
316d756449SSean Bruno #include <sys/ctype.h>
326d756449SSean Bruno #include <sys/sbuf.h>
336d756449SSean Bruno #include <sys/systm.h>
346d756449SSean Bruno #include <sys/sysproto.h>
356d756449SSean Bruno #include <sys/exec.h>
366d756449SSean Bruno #include <sys/imgact.h>
376d756449SSean Bruno #include <sys/imgact_binmisc.h>
386d756449SSean Bruno #include <sys/kernel.h>
396d756449SSean Bruno #include <sys/libkern.h>
406d756449SSean Bruno #include <sys/lock.h>
416d756449SSean Bruno #include <sys/malloc.h>
426d756449SSean Bruno #include <sys/mutex.h>
436d756449SSean Bruno #include <sys/sysctl.h>
445f98711dSSean Bruno #include <sys/sx.h>
455f98711dSSean Bruno 
465f98711dSSean Bruno #include <machine/atomic.h>
476d756449SSean Bruno 
486d756449SSean Bruno /**
496d756449SSean Bruno  * Miscellaneous binary interpreter image activator.
506d756449SSean Bruno  *
516d756449SSean Bruno  * If the given target executable's header matches 'xbe_magic' field in the
526d756449SSean Bruno  * 'interpreter_list' then it will use the user-level interpreter specified in
536d756449SSean Bruno  * the 'xbe_interpreter' field to execute the binary. The 'xbe_magic' field may
546d756449SSean Bruno  * be adjusted to a given offset using the value in the 'xbe_moffset' field
556d756449SSean Bruno  * and bits of the header may be masked using the 'xbe_mask' field.  The
566d756449SSean Bruno  * 'interpreter_list' entries are managed using sysctl(3) as described in the
576d756449SSean Bruno  * <sys/imgact_binmisc.h> file.
586d756449SSean Bruno  */
596d756449SSean Bruno 
606d756449SSean Bruno /*
616d756449SSean Bruno  * Node of the interpreter list.
626d756449SSean Bruno  */
636d756449SSean Bruno typedef struct imgact_binmisc_entry {
646d756449SSean Bruno 	char				 *ibe_name;
656d756449SSean Bruno 	uint8_t				 *ibe_magic;
666d756449SSean Bruno 	uint32_t			  ibe_moffset;
676d756449SSean Bruno 	uint32_t			  ibe_msize;
686d756449SSean Bruno 	uint8_t				 *ibe_mask;
696d756449SSean Bruno 	uint8_t				 *ibe_interpreter;
706d756449SSean Bruno 	uint32_t			  ibe_interp_argcnt;
716d756449SSean Bruno 	uint32_t			  ibe_interp_length;
726d756449SSean Bruno 	uint32_t			  ibe_flags;
736d756449SSean Bruno 	SLIST_ENTRY(imgact_binmisc_entry) link;
746d756449SSean Bruno } imgact_binmisc_entry_t;
756d756449SSean Bruno 
766d756449SSean Bruno /*
776d756449SSean Bruno  * sysctl() commands.
786d756449SSean Bruno  */
796d756449SSean Bruno #define IBC_ADD		1	/* Add given entry. */
806d756449SSean Bruno #define IBC_REMOVE	2	/* Remove entry for a given name. */
816d756449SSean Bruno #define IBC_DISABLE	3	/* Disable entry for a given name. */
826d756449SSean Bruno #define IBC_ENABLE	4	/* Enable entry for a given name. */
836d756449SSean Bruno #define IBC_LOOKUP	5	/* Lookup and return entry for given name. */
846d756449SSean Bruno #define IBC_LIST	6	/* Get a snapshot of the interpretor list. */
856d756449SSean Bruno 
866d756449SSean Bruno /*
876d756449SSean Bruno  * Interpreter string macros.
886d756449SSean Bruno  *
896d756449SSean Bruno  * They all start with '#' followed by a single letter:
906d756449SSean Bruno  */
916d756449SSean Bruno #define	ISM_POUND	'#'	/* "##" is the escape sequence for single #. */
926d756449SSean Bruno #define	ISM_OLD_ARGV0	'a'	/* "#a" is replaced with the old argv0. */
936d756449SSean Bruno 
946d756449SSean Bruno MALLOC_DEFINE(M_BINMISC, KMOD_NAME, "misc binary image activator");
956d756449SSean Bruno 
966d756449SSean Bruno /* The interpreter list. */
976d756449SSean Bruno static SLIST_HEAD(, imgact_binmisc_entry) interpreter_list =
986d756449SSean Bruno 	SLIST_HEAD_INITIALIZER(interpreter_list);
996d756449SSean Bruno 
1006d756449SSean Bruno static int interp_list_entry_count = 0;
101280b7169SSean Bruno 
1025f98711dSSean Bruno static struct sx interp_list_sx;
1036d756449SSean Bruno 
1046d756449SSean Bruno /*
1056d756449SSean Bruno  * Populate the entry with the information about the interpreter.
1066d756449SSean Bruno  */
1076d756449SSean Bruno static void
1086d756449SSean Bruno imgact_binmisc_populate_interp(char *str, imgact_binmisc_entry_t *ibe)
1096d756449SSean Bruno {
1106d756449SSean Bruno 	uint32_t len = 0, argc = 1;
1116d756449SSean Bruno 	char t[IBE_INTERP_LEN_MAX];
1126d756449SSean Bruno 	char *sp, *tp;
1136d756449SSean Bruno 
1145f98711dSSean Bruno 	memset(t, 0, sizeof(t));
1156d756449SSean Bruno 
1166d756449SSean Bruno 	/*
1176d756449SSean Bruno 	 * Normalize interpreter string. Replace white space between args with
1186d756449SSean Bruno 	 * single space.
1196d756449SSean Bruno 	 */
1206d756449SSean Bruno 	sp = str; tp = t;
1216d756449SSean Bruno 	while (*sp != '\0') {
1226d756449SSean Bruno 		if (*sp == ' ' || *sp == '\t') {
1236d756449SSean Bruno 			if (++len > IBE_INTERP_LEN_MAX)
1246d756449SSean Bruno 				break;
1256d756449SSean Bruno 			*tp++ = ' ';
1266d756449SSean Bruno 			argc++;
1276d756449SSean Bruno 			while (*sp == ' ' || *sp == '\t')
1286d756449SSean Bruno 				sp++;
1296d756449SSean Bruno 			continue;
1306d756449SSean Bruno 		} else {
1316d756449SSean Bruno 			*tp++ = *sp++;
1326d756449SSean Bruno 			len++;
1336d756449SSean Bruno 		}
1346d756449SSean Bruno 	}
1356d756449SSean Bruno 	*tp = '\0';
1366d756449SSean Bruno 	len++;
1376d756449SSean Bruno 
1386d756449SSean Bruno 	ibe->ibe_interpreter = malloc(len, M_BINMISC, M_WAITOK|M_ZERO);
1396d756449SSean Bruno 
1406d756449SSean Bruno 	/* Populate all the ibe fields for the interpreter. */
1416d756449SSean Bruno 	memcpy(ibe->ibe_interpreter, t, len);
1426d756449SSean Bruno 	ibe->ibe_interp_argcnt = argc;
1436d756449SSean Bruno 	ibe->ibe_interp_length = len;
1446d756449SSean Bruno }
1456d756449SSean Bruno 
1466d756449SSean Bruno /*
1476d756449SSean Bruno  * Allocate memory and populate a new entry for the interpreter table.
1486d756449SSean Bruno  */
1496d756449SSean Bruno static imgact_binmisc_entry_t *
1506d756449SSean Bruno imgact_binmisc_new_entry(ximgact_binmisc_entry_t *xbe)
1516d756449SSean Bruno {
1526d756449SSean Bruno 	imgact_binmisc_entry_t *ibe = NULL;
1536d756449SSean Bruno 	size_t namesz = min(strlen(xbe->xbe_name) + 1, IBE_NAME_MAX);
1546d756449SSean Bruno 
1556d756449SSean Bruno 	ibe = malloc(sizeof(*ibe), M_BINMISC, M_WAITOK|M_ZERO);
1566d756449SSean Bruno 
1576d756449SSean Bruno 	ibe->ibe_name = malloc(namesz, M_BINMISC, M_WAITOK|M_ZERO);
1586d756449SSean Bruno 	strlcpy(ibe->ibe_name, xbe->xbe_name, namesz);
1596d756449SSean Bruno 
1606d756449SSean Bruno 	imgact_binmisc_populate_interp(xbe->xbe_interpreter, ibe);
1616d756449SSean Bruno 
1626d756449SSean Bruno 	ibe->ibe_magic = malloc(xbe->xbe_msize, M_BINMISC, M_WAITOK|M_ZERO);
1636d756449SSean Bruno 	memcpy(ibe->ibe_magic, xbe->xbe_magic, xbe->xbe_msize);
1646d756449SSean Bruno 
1656d756449SSean Bruno 	ibe->ibe_mask = malloc(xbe->xbe_msize, M_BINMISC, M_WAITOK|M_ZERO);
1666d756449SSean Bruno 	memcpy(ibe->ibe_mask, xbe->xbe_mask, xbe->xbe_msize);
1676d756449SSean Bruno 
1686d756449SSean Bruno 	ibe->ibe_moffset = xbe->xbe_moffset;
1696d756449SSean Bruno 	ibe->ibe_msize = xbe->xbe_msize;
1706d756449SSean Bruno 	ibe->ibe_flags = xbe->xbe_flags;
1716d756449SSean Bruno 
1726d756449SSean Bruno 	return (ibe);
1736d756449SSean Bruno }
1746d756449SSean Bruno 
1756d756449SSean Bruno /*
1766d756449SSean Bruno  * Free the allocated memory for a given list item.
1776d756449SSean Bruno  */
1786d756449SSean Bruno static void
1796d756449SSean Bruno imgact_binmisc_destroy_entry(imgact_binmisc_entry_t *ibe)
1806d756449SSean Bruno {
1816d756449SSean Bruno 	if (!ibe)
1826d756449SSean Bruno 		return;
183b888dae4SSean Bruno 	if (ibe->ibe_magic)
1846d756449SSean Bruno 		free(ibe->ibe_magic, M_BINMISC);
1856d756449SSean Bruno 	if (ibe->ibe_mask)
1866d756449SSean Bruno 		free(ibe->ibe_mask, M_BINMISC);
1876d756449SSean Bruno 	if (ibe->ibe_interpreter)
1886d756449SSean Bruno 		free(ibe->ibe_interpreter, M_BINMISC);
1896d756449SSean Bruno 	if (ibe->ibe_name)
1906d756449SSean Bruno 		free(ibe->ibe_name, M_BINMISC);
1916d756449SSean Bruno 	if (ibe)
1926d756449SSean Bruno 		free(ibe, M_BINMISC);
1936d756449SSean Bruno }
1946d756449SSean Bruno 
1956d756449SSean Bruno /*
1966d756449SSean Bruno  * Find the interpreter in the list by the given name.  Return NULL if not
1976d756449SSean Bruno  * found.
1986d756449SSean Bruno  */
1996d756449SSean Bruno static imgact_binmisc_entry_t *
2006d756449SSean Bruno imgact_binmisc_find_entry(char *name)
2016d756449SSean Bruno {
2026d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
2036d756449SSean Bruno 
2045f98711dSSean Bruno 	sx_assert(&interp_list_sx, SA_LOCKED);
2056d756449SSean Bruno 
2066d756449SSean Bruno 	SLIST_FOREACH(ibe, &interpreter_list, link) {
2076d756449SSean Bruno 		if (strncmp(name, ibe->ibe_name, IBE_NAME_MAX) == 0)
2086d756449SSean Bruno 			return (ibe);
2096d756449SSean Bruno 	}
2106d756449SSean Bruno 
2116d756449SSean Bruno 	return (NULL);
2126d756449SSean Bruno }
2136d756449SSean Bruno 
2146d756449SSean Bruno /*
2156d756449SSean Bruno  * Add the given interpreter if it doesn't already exist.  Return EEXIST
2166d756449SSean Bruno  * if the name already exist in the interpreter list.
2176d756449SSean Bruno  */
2186d756449SSean Bruno static int
2196d756449SSean Bruno imgact_binmisc_add_entry(ximgact_binmisc_entry_t *xbe)
2206d756449SSean Bruno {
2216d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
2226d756449SSean Bruno 	char *p;
2236d756449SSean Bruno 
2246d756449SSean Bruno 	if (xbe->xbe_msize > IBE_MAGIC_MAX)
2256d756449SSean Bruno 		return (EINVAL);
2266d756449SSean Bruno 
2276d756449SSean Bruno 	for(p = xbe->xbe_name; *p != 0; p++)
2286d756449SSean Bruno 		if (!isascii((int)*p))
2296d756449SSean Bruno 			return (EINVAL);
2306d756449SSean Bruno 
2316d756449SSean Bruno 	for(p = xbe->xbe_interpreter; *p != 0; p++)
2326d756449SSean Bruno 		if (!isascii((int)*p))
2336d756449SSean Bruno 			return (EINVAL);
2346d756449SSean Bruno 
2356d756449SSean Bruno 	/* Make sure we don't have any invalid #'s. */
2366d756449SSean Bruno 	p = xbe->xbe_interpreter;
2376d756449SSean Bruno 	while (1) {
2386d756449SSean Bruno 		p = strchr(p, '#');
2396d756449SSean Bruno 		if (!p)
2406d756449SSean Bruno 			break;
2416d756449SSean Bruno 
2426d756449SSean Bruno 		p++;
2436d756449SSean Bruno 		switch(*p) {
2446d756449SSean Bruno 		case ISM_POUND:
2456d756449SSean Bruno 			/* "##" */
2466d756449SSean Bruno 			p++;
2476d756449SSean Bruno 			break;
2486d756449SSean Bruno 
2496d756449SSean Bruno 		case ISM_OLD_ARGV0:
2506d756449SSean Bruno 			/* "#a" */
2516d756449SSean Bruno 			p++;
2526d756449SSean Bruno 			break;
2536d756449SSean Bruno 
2546d756449SSean Bruno 		case 0:
2556d756449SSean Bruno 		default:
2566d756449SSean Bruno 			/* Anything besides the above is invalid. */
2576d756449SSean Bruno 			return (EINVAL);
2586d756449SSean Bruno 		}
2596d756449SSean Bruno 	}
2606d756449SSean Bruno 
2615f98711dSSean Bruno 	sx_xlock(&interp_list_sx);
262280b7169SSean Bruno 	if (imgact_binmisc_find_entry(xbe->xbe_name) != NULL) {
2635f98711dSSean Bruno 		sx_xunlock(&interp_list_sx);
264280b7169SSean Bruno 		return (EEXIST);
265280b7169SSean Bruno 	}
266280b7169SSean Bruno 
2675f98711dSSean Bruno 	/* Preallocate a new entry. */
2686d756449SSean Bruno 	ibe = imgact_binmisc_new_entry(xbe);
2696d756449SSean Bruno 	if (!ibe)
2706d756449SSean Bruno 		return (ENOMEM);
2716d756449SSean Bruno 
2726d756449SSean Bruno 	SLIST_INSERT_HEAD(&interpreter_list, ibe, link);
2736d756449SSean Bruno 	interp_list_entry_count++;
2745f98711dSSean Bruno 	sx_xunlock(&interp_list_sx);
2756d756449SSean Bruno 
2766d756449SSean Bruno 	return (0);
2776d756449SSean Bruno }
2786d756449SSean Bruno 
2796d756449SSean Bruno /*
2806d756449SSean Bruno  * Remove the interpreter in the list with the given name. Return ENOENT
2816d756449SSean Bruno  * if not found.
2826d756449SSean Bruno  */
2836d756449SSean Bruno static int
2846d756449SSean Bruno imgact_binmisc_remove_entry(char *name)
2856d756449SSean Bruno {
2866d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
2876d756449SSean Bruno 
2885f98711dSSean Bruno 	sx_xlock(&interp_list_sx);
2896d756449SSean Bruno 	if ((ibe = imgact_binmisc_find_entry(name)) == NULL) {
2905f98711dSSean Bruno 		sx_xunlock(&interp_list_sx);
2916d756449SSean Bruno 		return (ENOENT);
2926d756449SSean Bruno 	}
2936d756449SSean Bruno 	SLIST_REMOVE(&interpreter_list, ibe, imgact_binmisc_entry, link);
2946d756449SSean Bruno 	interp_list_entry_count--;
2955f98711dSSean Bruno 	sx_xunlock(&interp_list_sx);
2966d756449SSean Bruno 
2976d756449SSean Bruno 	imgact_binmisc_destroy_entry(ibe);
2986d756449SSean Bruno 
2996d756449SSean Bruno 	return (0);
3006d756449SSean Bruno }
3016d756449SSean Bruno 
3026d756449SSean Bruno /*
3036d756449SSean Bruno  * Disable the interpreter in the list with the given name. Return ENOENT
3046d756449SSean Bruno  * if not found.
3056d756449SSean Bruno  */
3066d756449SSean Bruno static int
3076d756449SSean Bruno imgact_binmisc_disable_entry(char *name)
3086d756449SSean Bruno {
3096d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
3106d756449SSean Bruno 
311*4e83b32aSSean Bruno 	sx_xlock(&interp_list_sx);
3126d756449SSean Bruno 	if ((ibe = imgact_binmisc_find_entry(name)) == NULL) {
313*4e83b32aSSean Bruno 		sx_xunlock(&interp_list_sx);
3146d756449SSean Bruno 		return (ENOENT);
3156d756449SSean Bruno 	}
3166d756449SSean Bruno 
317*4e83b32aSSean Bruno 	ibe->ibe_flags &= ~IBF_ENABLED;
318*4e83b32aSSean Bruno 	sx_xunlock(&interp_list_sx);
3196d756449SSean Bruno 
3206d756449SSean Bruno 	return (0);
3216d756449SSean Bruno }
3226d756449SSean Bruno 
3236d756449SSean Bruno /*
3246d756449SSean Bruno  * Enable the interpreter in the list with the given name. Return ENOENT
3256d756449SSean Bruno  * if not found.
3266d756449SSean Bruno  */
3276d756449SSean Bruno static int
3286d756449SSean Bruno imgact_binmisc_enable_entry(char *name)
3296d756449SSean Bruno {
3306d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
3316d756449SSean Bruno 
332*4e83b32aSSean Bruno 	sx_xlock(&interp_list_sx);
3336d756449SSean Bruno 	if ((ibe = imgact_binmisc_find_entry(name)) == NULL) {
334*4e83b32aSSean Bruno 		sx_xunlock(&interp_list_sx);
3356d756449SSean Bruno 		return (ENOENT);
3366d756449SSean Bruno 	}
3376d756449SSean Bruno 
338*4e83b32aSSean Bruno 	ibe->ibe_flags |= IBF_ENABLED;
339*4e83b32aSSean Bruno 	sx_xunlock(&interp_list_sx);
3406d756449SSean Bruno 
3416d756449SSean Bruno 	return (0);
3426d756449SSean Bruno }
3436d756449SSean Bruno 
3446d756449SSean Bruno static int
3456d756449SSean Bruno imgact_binmisc_populate_xbe(ximgact_binmisc_entry_t *xbe,
3466d756449SSean Bruno     imgact_binmisc_entry_t *ibe)
3476d756449SSean Bruno {
3486d756449SSean Bruno 	uint32_t i;
3496d756449SSean Bruno 
3505f98711dSSean Bruno 	sx_assert(&interp_list_sx, SA_LOCKED);
3516d756449SSean Bruno 
3525f98711dSSean Bruno 	memset(xbe, 0, sizeof(*xbe));
3536d756449SSean Bruno 	strlcpy(xbe->xbe_name, ibe->ibe_name, IBE_NAME_MAX);
3546d756449SSean Bruno 
3556d756449SSean Bruno 	/* Copy interpreter string.  Replace NULL breaks with space. */
3566d756449SSean Bruno 	memcpy(xbe->xbe_interpreter, ibe->ibe_interpreter,
3576d756449SSean Bruno 	    ibe->ibe_interp_length);
3586d756449SSean Bruno 	for(i = 0; i < (ibe->ibe_interp_length - 1); i++)
3596d756449SSean Bruno 		if (xbe->xbe_interpreter[i] == '\0')
3606d756449SSean Bruno 			xbe->xbe_interpreter[i] = ' ';
3616d756449SSean Bruno 
3626d756449SSean Bruno 	memcpy(xbe->xbe_magic, ibe->ibe_magic, ibe->ibe_msize);
3636d756449SSean Bruno 	memcpy(xbe->xbe_mask, ibe->ibe_mask, ibe->ibe_msize);
3646d756449SSean Bruno 	xbe->xbe_version = IBE_VERSION;
3656d756449SSean Bruno 	xbe->xbe_flags = ibe->ibe_flags;
3666d756449SSean Bruno 	xbe->xbe_moffset = ibe->ibe_moffset;
3676d756449SSean Bruno 	xbe->xbe_msize = ibe->ibe_msize;
3686d756449SSean Bruno 
3696d756449SSean Bruno 	return (0);
3706d756449SSean Bruno }
3716d756449SSean Bruno 
3726d756449SSean Bruno /*
3736d756449SSean Bruno  * Retrieve the interpreter with the give name and populate the
3746d756449SSean Bruno  * ximgact_binmisc_entry structure.  Return ENOENT if not found.
3756d756449SSean Bruno  */
3766d756449SSean Bruno static int
3776d756449SSean Bruno imgact_binmisc_lookup_entry(char *name, ximgact_binmisc_entry_t *xbe)
3786d756449SSean Bruno {
3796d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
3806d756449SSean Bruno 	int error = 0;
3816d756449SSean Bruno 
3825f98711dSSean Bruno 	sx_slock(&interp_list_sx);
3836d756449SSean Bruno 	if ((ibe = imgact_binmisc_find_entry(name)) == NULL) {
3845f98711dSSean Bruno 		sx_sunlock(&interp_list_sx);
3856d756449SSean Bruno 		return (ENOENT);
3866d756449SSean Bruno 	}
3876d756449SSean Bruno 
3886d756449SSean Bruno 	error = imgact_binmisc_populate_xbe(xbe, ibe);
3895f98711dSSean Bruno 	sx_sunlock(&interp_list_sx);
3906d756449SSean Bruno 
3916d756449SSean Bruno 	return (error);
3926d756449SSean Bruno }
3936d756449SSean Bruno 
3946d756449SSean Bruno /*
3956d756449SSean Bruno  * Get a snapshot of all the interpreter entries in the list.
3966d756449SSean Bruno  */
3976d756449SSean Bruno static int
3986d756449SSean Bruno imgact_binmisc_get_all_entries(struct sysctl_req *req)
3996d756449SSean Bruno {
4006d756449SSean Bruno 	ximgact_binmisc_entry_t *xbe, *xbep;
4016d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
4026d756449SSean Bruno 	int error = 0, count;
4036d756449SSean Bruno 
4045f98711dSSean Bruno 	sx_slock(&interp_list_sx);
4056d756449SSean Bruno 	count = interp_list_entry_count;
406e0ae213fSSean Bruno 	xbe = malloc(sizeof(*xbe) * count, M_BINMISC, M_WAITOK|M_ZERO);
4076d756449SSean Bruno 
4086d756449SSean Bruno 	xbep = xbe;
4096d756449SSean Bruno 	SLIST_FOREACH(ibe, &interpreter_list, link) {
4106d756449SSean Bruno 		error = imgact_binmisc_populate_xbe(xbep++, ibe);
4116d756449SSean Bruno 		if (error)
4126d756449SSean Bruno 			break;
4136d756449SSean Bruno 	}
4145f98711dSSean Bruno 	sx_sunlock(&interp_list_sx);
4156d756449SSean Bruno 
4166d756449SSean Bruno 	if (!error)
4176d756449SSean Bruno 		error = SYSCTL_OUT(req, xbe, sizeof(*xbe) * count);
4186d756449SSean Bruno 
4196d756449SSean Bruno 	free(xbe, M_BINMISC);
4206d756449SSean Bruno 	return (error);
4216d756449SSean Bruno }
4226d756449SSean Bruno 
4236d756449SSean Bruno /*
4246d756449SSean Bruno  * sysctl() handler for munipulating interpretor table.
4256d756449SSean Bruno  * Not MP safe (locked by sysctl).
4266d756449SSean Bruno  */
4276d756449SSean Bruno static int
4286d756449SSean Bruno sysctl_kern_binmisc(SYSCTL_HANDLER_ARGS)
4296d756449SSean Bruno {
4306d756449SSean Bruno 	ximgact_binmisc_entry_t xbe;
4316d756449SSean Bruno 	int error = 0;
4326d756449SSean Bruno 
4336d756449SSean Bruno 	switch(arg2) {
4346d756449SSean Bruno 	case IBC_ADD:
4356d756449SSean Bruno 		/* Add an entry. Limited to IBE_MAX_ENTRIES. */
4366d756449SSean Bruno 		error = SYSCTL_IN(req, &xbe, sizeof(xbe));
4376d756449SSean Bruno 		if (error)
4386d756449SSean Bruno 			return (error);
4396d756449SSean Bruno 		if (IBE_VERSION != xbe.xbe_version)
4406d756449SSean Bruno 			return (EINVAL);
4416d756449SSean Bruno 		if (interp_list_entry_count == IBE_MAX_ENTRIES)
4426d756449SSean Bruno 			return (ENOSPC);
4436d756449SSean Bruno 		error = imgact_binmisc_add_entry(&xbe);
4446d756449SSean Bruno 		break;
4456d756449SSean Bruno 
4466d756449SSean Bruno 	case IBC_REMOVE:
4476d756449SSean Bruno 		/* Remove an entry. */
4486d756449SSean Bruno 		error = SYSCTL_IN(req, &xbe, sizeof(xbe));
4496d756449SSean Bruno 		if (error)
4506d756449SSean Bruno 			return (error);
4516d756449SSean Bruno 		if (IBE_VERSION != xbe.xbe_version)
4526d756449SSean Bruno 			return (EINVAL);
4536d756449SSean Bruno 		error = imgact_binmisc_remove_entry(xbe.xbe_name);
4546d756449SSean Bruno 		break;
4556d756449SSean Bruno 
4566d756449SSean Bruno 	case IBC_DISABLE:
4576d756449SSean Bruno 		/* Disable an entry. */
4586d756449SSean Bruno 		error = SYSCTL_IN(req, &xbe, sizeof(xbe));
4596d756449SSean Bruno 		if (error)
4606d756449SSean Bruno 			return (error);
4616d756449SSean Bruno 		if (IBE_VERSION != xbe.xbe_version)
4626d756449SSean Bruno 			return (EINVAL);
4636d756449SSean Bruno 		error = imgact_binmisc_disable_entry(xbe.xbe_name);
4646d756449SSean Bruno 		break;
4656d756449SSean Bruno 
4666d756449SSean Bruno 	case IBC_ENABLE:
4676d756449SSean Bruno 		/* Enable an entry. */
4686d756449SSean Bruno 		error = SYSCTL_IN(req, &xbe, sizeof(xbe));
4696d756449SSean Bruno 		if (error)
4706d756449SSean Bruno 			return (error);
4716d756449SSean Bruno 		if (IBE_VERSION != xbe.xbe_version)
4726d756449SSean Bruno 			return (EINVAL);
4736d756449SSean Bruno 		error = imgact_binmisc_enable_entry(xbe.xbe_name);
4746d756449SSean Bruno 		break;
4756d756449SSean Bruno 
4766d756449SSean Bruno 	case IBC_LOOKUP:
4776d756449SSean Bruno 		/* Lookup an entry. */
4786d756449SSean Bruno 		error = SYSCTL_IN(req, &xbe, sizeof(xbe));
4796d756449SSean Bruno 		if (error)
4806d756449SSean Bruno 			return (error);
4816d756449SSean Bruno 		if (IBE_VERSION != xbe.xbe_version)
4826d756449SSean Bruno 			return (EINVAL);
4836d756449SSean Bruno 		error = imgact_binmisc_lookup_entry(xbe.xbe_name, &xbe);
4846d756449SSean Bruno 		if (!error)
4856d756449SSean Bruno 			error = SYSCTL_OUT(req, &xbe, sizeof(xbe));
4866d756449SSean Bruno 		break;
4876d756449SSean Bruno 
4886d756449SSean Bruno 	case IBC_LIST:
4896d756449SSean Bruno 		/* Return a snapshot of the interpretor list. */
4906d756449SSean Bruno 
4916d756449SSean Bruno 		if (!req->oldptr) {
4926d756449SSean Bruno 			/* No pointer then just return the list size. */
4936d756449SSean Bruno 			error = SYSCTL_OUT(req, 0, interp_list_entry_count *
4946d756449SSean Bruno 			    sizeof(ximgact_binmisc_entry_t));
4956d756449SSean Bruno 			return (error);
4966d756449SSean Bruno 		} else
4976d756449SSean Bruno 			if (!req->oldlen)
4986d756449SSean Bruno 				return (EINVAL);
4996d756449SSean Bruno 
5006d756449SSean Bruno 		error = imgact_binmisc_get_all_entries(req);
5016d756449SSean Bruno 		break;
5026d756449SSean Bruno 
5036d756449SSean Bruno 	default:
5046d756449SSean Bruno 		return (EINVAL);
5056d756449SSean Bruno 	}
5066d756449SSean Bruno 
5076d756449SSean Bruno 	return (error);
5086d756449SSean Bruno }
5096d756449SSean Bruno 
5106d756449SSean Bruno SYSCTL_NODE(_kern, OID_AUTO, binmisc, CTLFLAG_RW, 0,
5116d756449SSean Bruno     "Image activator for miscellaneous binaries");
5126d756449SSean Bruno 
5136d756449SSean Bruno SYSCTL_PROC(_kern_binmisc, OID_AUTO, add,
5146d756449SSean Bruno     CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_ADD,
5156d756449SSean Bruno     sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
5166d756449SSean Bruno     "Add an activator entry");
5176d756449SSean Bruno 
5186d756449SSean Bruno SYSCTL_PROC(_kern_binmisc, OID_AUTO, remove,
5196d756449SSean Bruno     CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_REMOVE,
5206d756449SSean Bruno     sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
5216d756449SSean Bruno     "Remove an activator entry");
5226d756449SSean Bruno 
5236d756449SSean Bruno SYSCTL_PROC(_kern_binmisc, OID_AUTO, disable,
5246d756449SSean Bruno     CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_DISABLE,
5256d756449SSean Bruno     sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
5266d756449SSean Bruno     "Disable an activator entry");
5276d756449SSean Bruno 
5286d756449SSean Bruno SYSCTL_PROC(_kern_binmisc, OID_AUTO, enable,
5296d756449SSean Bruno     CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_WR, NULL, IBC_ENABLE,
5306d756449SSean Bruno     sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
5316d756449SSean Bruno     "Enable an activator entry");
5326d756449SSean Bruno 
5336d756449SSean Bruno SYSCTL_PROC(_kern_binmisc, OID_AUTO, lookup,
5346d756449SSean Bruno     CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_RW|CTLFLAG_ANYBODY, NULL, IBC_LOOKUP,
5356d756449SSean Bruno     sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
5366d756449SSean Bruno     "Lookup an activator entry");
5376d756449SSean Bruno 
5386d756449SSean Bruno SYSCTL_PROC(_kern_binmisc, OID_AUTO, list,
5396d756449SSean Bruno     CTLFLAG_MPSAFE|CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_ANYBODY, NULL, IBC_LIST,
5406d756449SSean Bruno     sysctl_kern_binmisc, "S,ximgact_binmisc_entry",
5416d756449SSean Bruno     "Get snapshot of all the activator entries");
5426d756449SSean Bruno 
5436d756449SSean Bruno static imgact_binmisc_entry_t *
5446d756449SSean Bruno imgact_binmisc_find_interpreter(const char *image_header)
5456d756449SSean Bruno {
5466d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
5476d756449SSean Bruno 	const char *p;
5486d756449SSean Bruno 	int i;
5496d756449SSean Bruno 	size_t sz;
5506d756449SSean Bruno 
5515f98711dSSean Bruno 	sx_assert(&interp_list_sx, SA_LOCKED);
5526d756449SSean Bruno 
5536d756449SSean Bruno 	SLIST_FOREACH(ibe, &interpreter_list, link) {
5546d756449SSean Bruno 		if (!(IBF_ENABLED & ibe->ibe_flags))
5556d756449SSean Bruno 			continue;
5566d756449SSean Bruno 
5576d756449SSean Bruno 		p = image_header + ibe->ibe_moffset;
5586d756449SSean Bruno 		sz = ibe->ibe_msize;
5596d756449SSean Bruno 		if (IBF_USE_MASK & ibe->ibe_flags) {
5606d756449SSean Bruno 			/* Compare using mask. */
5616d756449SSean Bruno 			for (i = 0; i < sz; i++)
5626d756449SSean Bruno 				if ((*p++ ^ ibe->ibe_magic[i]) &
5636d756449SSean Bruno 				    ibe->ibe_mask[i])
5646d756449SSean Bruno 					break;
5656d756449SSean Bruno 		} else {
5666d756449SSean Bruno 			for (i = 0; i < sz; i++)
5676d756449SSean Bruno 				if (*p++ ^ ibe->ibe_magic[i])
5686d756449SSean Bruno 					break;
5696d756449SSean Bruno 		}
5706d756449SSean Bruno 		if (i == ibe->ibe_msize)
5716d756449SSean Bruno 			return (ibe);
5726d756449SSean Bruno 	}
5736d756449SSean Bruno 	return (NULL);
5746d756449SSean Bruno }
5756d756449SSean Bruno 
576945afa7cSSean Bruno static int
5776d756449SSean Bruno imgact_binmisc_exec(struct image_params *imgp)
5786d756449SSean Bruno {
5796d756449SSean Bruno 	const char *image_header = imgp->image_header;
5806d756449SSean Bruno 	const char *fname = NULL;
5816d756449SSean Bruno 	int error = 0;
5826d756449SSean Bruno 	size_t offset, l;
5836d756449SSean Bruno 	imgact_binmisc_entry_t *ibe;
5846d756449SSean Bruno 	struct sbuf *sname;
5856d756449SSean Bruno 	char *s, *d;
5866d756449SSean Bruno 
5876d756449SSean Bruno 	/* Do we have an interpreter for the given image header? */
5885f98711dSSean Bruno 	sx_slock(&interp_list_sx);
5896d756449SSean Bruno 	if ((ibe = imgact_binmisc_find_interpreter(image_header)) == NULL) {
5905f98711dSSean Bruno 		sx_sunlock(&interp_list_sx);
5916d756449SSean Bruno 		return (-1);
5926d756449SSean Bruno 	}
5936d756449SSean Bruno 
5946d756449SSean Bruno 	/* No interpreter nesting allowed. */
59565f20a89SSean Bruno 	if (imgp->interpreted & IMGACT_BINMISC) {
5965f98711dSSean Bruno 		sx_sunlock(&interp_list_sx);
5976d756449SSean Bruno 		return (ENOEXEC);
5986d756449SSean Bruno 	}
5996d756449SSean Bruno 
60065f20a89SSean Bruno 	imgp->interpreted |= IMGACT_BINMISC;
6016d756449SSean Bruno 
6026d756449SSean Bruno 	if (imgp->args->fname != NULL) {
6036d756449SSean Bruno 		fname = imgp->args->fname;
6046d756449SSean Bruno 		sname = NULL;
6056d756449SSean Bruno 	} else {
6066d756449SSean Bruno 		/* Use the fdescfs(5) path for fexecve(2). */
6076d756449SSean Bruno 		sname = sbuf_new_auto();
6086d756449SSean Bruno 		sbuf_printf(sname, "/dev/fd/%d", imgp->args->fd);
6096d756449SSean Bruno 		sbuf_finish(sname);
6106d756449SSean Bruno 		fname = sbuf_data(sname);
6116d756449SSean Bruno 	}
6126d756449SSean Bruno 
6136d756449SSean Bruno 
6146d756449SSean Bruno 	/*
6156d756449SSean Bruno 	 * We need to "push" the interpreter in the arg[] list.  To do this,
6166d756449SSean Bruno 	 * we first shift all the other values in the `begin_argv' area to
6176d756449SSean Bruno 	 * provide the exact amount of room for the values added.  Set up
6186d756449SSean Bruno 	 * `offset' as the number of bytes to be added to the `begin_argv'
6196d756449SSean Bruno 	 * area.
6206d756449SSean Bruno 	 */
6216d756449SSean Bruno 	offset = ibe->ibe_interp_length;
6226d756449SSean Bruno 
6236d756449SSean Bruno 	/* Adjust the offset for #'s. */
6246d756449SSean Bruno 	s = ibe->ibe_interpreter;
6256d756449SSean Bruno 	while (1) {
6266d756449SSean Bruno 		s = strchr(s, '#');
6276d756449SSean Bruno 		if (!s)
6286d756449SSean Bruno 			break;
6296d756449SSean Bruno 
6306d756449SSean Bruno 		s++;
6316d756449SSean Bruno 		switch(*s) {
6326d756449SSean Bruno 		case ISM_POUND:
6336d756449SSean Bruno 			/* "##" -> "#": reduce offset by one. */
6346d756449SSean Bruno 			offset--;
6356d756449SSean Bruno 			break;
6366d756449SSean Bruno 
6376d756449SSean Bruno 		case ISM_OLD_ARGV0:
6386d756449SSean Bruno 			/* "#a" -> (old argv0): increase offset to fit fname */
6396d756449SSean Bruno 			offset += strlen(fname) - 2;
6406d756449SSean Bruno 			break;
6416d756449SSean Bruno 
6426d756449SSean Bruno 		default:
6436d756449SSean Bruno 			/* Hmm... This shouldn't happen. */
6445f98711dSSean Bruno 			sx_sunlock(&interp_list_sx);
6456d756449SSean Bruno 			printf("%s: Unknown macro #%c sequence in "
6466d756449SSean Bruno 			    "interpreter string\n", KMOD_NAME, *(s + 1));
6476d756449SSean Bruno 			error = EINVAL;
6486d756449SSean Bruno 			goto done;
6496d756449SSean Bruno 		}
6506d756449SSean Bruno 		s++;
6516d756449SSean Bruno 	}
6526d756449SSean Bruno 
6536d756449SSean Bruno 	/* Check to make sure we won't overrun the stringspace. */
6546d756449SSean Bruno 	if (offset > imgp->args->stringspace) {
6555f98711dSSean Bruno 		sx_sunlock(&interp_list_sx);
6566d756449SSean Bruno 		error = E2BIG;
6576d756449SSean Bruno 		goto done;
6586d756449SSean Bruno 	}
6596d756449SSean Bruno 
660280b7169SSean Bruno 	/* Make room for the interpreter */
6616d756449SSean Bruno 	bcopy(imgp->args->begin_argv, imgp->args->begin_argv + offset,
6626d756449SSean Bruno 	    imgp->args->endp - imgp->args->begin_argv);
6636d756449SSean Bruno 
6646d756449SSean Bruno 	/* Adjust everything by the offset. */
6656d756449SSean Bruno 	imgp->args->begin_envv += offset;
6666d756449SSean Bruno 	imgp->args->endp += offset;
6676d756449SSean Bruno 	imgp->args->stringspace -= offset;
6686d756449SSean Bruno 
6696d756449SSean Bruno 	/* Add the new argument(s) in the count. */
6706d756449SSean Bruno 	imgp->args->argc += ibe->ibe_interp_argcnt;
6716d756449SSean Bruno 
6726d756449SSean Bruno 	/*
6736d756449SSean Bruno 	 * The original arg[] list has been shifted appropriately.  Copy in
6746d756449SSean Bruno 	 * the interpreter path.
6756d756449SSean Bruno 	 */
6766d756449SSean Bruno 	s = ibe->ibe_interpreter;
6776d756449SSean Bruno 	d = imgp->args->begin_argv;
6786d756449SSean Bruno 	while(*s != '\0') {
6796d756449SSean Bruno 		switch (*s) {
6806d756449SSean Bruno 		case '#':
6816d756449SSean Bruno 			/* Handle "#" in interpreter string. */
6826d756449SSean Bruno 			s++;
6836d756449SSean Bruno 			switch(*s) {
6846d756449SSean Bruno 			case ISM_POUND:
6856d756449SSean Bruno 				/* "##": Replace with a single '#' */
6866d756449SSean Bruno 				*d++ = '#';
6876d756449SSean Bruno 				break;
6886d756449SSean Bruno 
6896d756449SSean Bruno 			case ISM_OLD_ARGV0:
6906d756449SSean Bruno 				/* "#a": Replace with old arg0 (fname). */
6916d756449SSean Bruno 				if ((l = strlen(fname)) != 0) {
6926d756449SSean Bruno 					memcpy(d, fname, l);
6936d756449SSean Bruno 					d += l;
6946d756449SSean Bruno 				}
6956d756449SSean Bruno 				break;
6966d756449SSean Bruno 
6976d756449SSean Bruno 			default:
6986d756449SSean Bruno 				/* Shouldn't happen but skip it if it does. */
6996d756449SSean Bruno 				break;
7006d756449SSean Bruno 			}
7016d756449SSean Bruno 			break;
7026d756449SSean Bruno 
7036d756449SSean Bruno 		case ' ':
7046d756449SSean Bruno 			/* Replace space with NUL to seperate arguments. */
7056d756449SSean Bruno 			*d++ = '\0';
7066d756449SSean Bruno 			break;
7076d756449SSean Bruno 
7086d756449SSean Bruno 		default:
7096d756449SSean Bruno 			*d++ = *s;
7106d756449SSean Bruno 			break;
7116d756449SSean Bruno 		}
7126d756449SSean Bruno 		s++;
7136d756449SSean Bruno 	}
7146d756449SSean Bruno 	*d = '\0';
7155f98711dSSean Bruno 	sx_sunlock(&interp_list_sx);
7166d756449SSean Bruno 
7176d756449SSean Bruno 	if (!error)
7186d756449SSean Bruno 		imgp->interpreter_name = imgp->args->begin_argv;
7196d756449SSean Bruno 
7206d756449SSean Bruno 
7216d756449SSean Bruno done:
7226d756449SSean Bruno 	if (sname)
7236d756449SSean Bruno 		sbuf_delete(sname);
7246d756449SSean Bruno 	return (error);
7256d756449SSean Bruno }
7266d756449SSean Bruno 
7276d756449SSean Bruno static void
7286d756449SSean Bruno imgact_binmisc_init(void *arg)
7296d756449SSean Bruno {
7306d756449SSean Bruno 
7315f98711dSSean Bruno 	sx_init(&interp_list_sx, KMOD_NAME);
7326d756449SSean Bruno }
7336d756449SSean Bruno 
7346d756449SSean Bruno static void
7356d756449SSean Bruno imgact_binmisc_fini(void *arg)
7366d756449SSean Bruno {
7376d756449SSean Bruno 	imgact_binmisc_entry_t *ibe, *ibe_tmp;
7386d756449SSean Bruno 
7396d756449SSean Bruno 	/* Free all the interpreters. */
7405f98711dSSean Bruno 	sx_xlock(&interp_list_sx);
7416d756449SSean Bruno 	SLIST_FOREACH_SAFE(ibe, &interpreter_list, link, ibe_tmp) {
7426d756449SSean Bruno 		SLIST_REMOVE(&interpreter_list, ibe, imgact_binmisc_entry,
7436d756449SSean Bruno 		    link);
7446d756449SSean Bruno 		imgact_binmisc_destroy_entry(ibe);
7456d756449SSean Bruno 	}
7465f98711dSSean Bruno 	sx_xunlock(&interp_list_sx);
7476d756449SSean Bruno 
7485f98711dSSean Bruno 	sx_destroy(&interp_list_sx);
7496d756449SSean Bruno }
7506d756449SSean Bruno 
7516d756449SSean Bruno SYSINIT(imgact_binmisc, SI_SUB_EXEC, SI_ORDER_MIDDLE, imgact_binmisc_init, 0);
7526d756449SSean Bruno SYSUNINIT(imgact_binmisc, SI_SUB_EXEC, SI_ORDER_MIDDLE, imgact_binmisc_fini, 0);
7536d756449SSean Bruno 
7546d756449SSean Bruno /*
7556d756449SSean Bruno  * Tell kern_execve.c about it, with a little help from the linker.
7566d756449SSean Bruno  */
7576d756449SSean Bruno static struct execsw imgact_binmisc_execsw = { imgact_binmisc_exec, KMOD_NAME };
7586d756449SSean Bruno EXEC_SET(imgact_binmisc, imgact_binmisc_execsw);
759