xref: /freebsd/lib/libveriexec/exec_script.c (revision 1554ba03b651319ab0e1cde8492ea4516afc648b)
1*1554ba03SSimon J. Gerraty /*
2*1554ba03SSimon J. Gerraty  * SPDX-License-Identifier: BSD-2-Clause
3*1554ba03SSimon J. Gerraty  *
4*1554ba03SSimon J. Gerraty  * Copyright (c) 2019-2023, Juniper Networks, Inc.
5*1554ba03SSimon J. Gerraty  * All rights reserved.
6*1554ba03SSimon J. Gerraty  *
7*1554ba03SSimon J. Gerraty  * Redistribution and use in source and binary forms, with or without
8*1554ba03SSimon J. Gerraty  * modification, are permitted provided that the following conditions
9*1554ba03SSimon J. Gerraty  * are met:
10*1554ba03SSimon J. Gerraty  * 1. Redistributions of source code must retain the above copyright
11*1554ba03SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer.
12*1554ba03SSimon J. Gerraty  * 2. Redistributions in binary form must reproduce the above copyright
13*1554ba03SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer in the
14*1554ba03SSimon J. Gerraty  *    documentation and/or other materials provided with the distribution.
15*1554ba03SSimon J. Gerraty  *
16*1554ba03SSimon J. Gerraty  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*1554ba03SSimon J. Gerraty  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*1554ba03SSimon J. Gerraty  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*1554ba03SSimon J. Gerraty  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*1554ba03SSimon J. Gerraty  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*1554ba03SSimon J. Gerraty  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*1554ba03SSimon J. Gerraty  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*1554ba03SSimon J. Gerraty  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*1554ba03SSimon J. Gerraty  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*1554ba03SSimon J. Gerraty  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*1554ba03SSimon J. Gerraty  * SUCH DAMAGE.
27*1554ba03SSimon J. Gerraty  */
28*1554ba03SSimon J. Gerraty 
29*1554ba03SSimon J. Gerraty #include <sys/types.h>
30*1554ba03SSimon J. Gerraty #include <sys/param.h>
31*1554ba03SSimon J. Gerraty #include <sys/errno.h>
32*1554ba03SSimon J. Gerraty #include <sys/mac.h>
33*1554ba03SSimon J. Gerraty 
34*1554ba03SSimon J. Gerraty #include <unistd.h>
35*1554ba03SSimon J. Gerraty #include <fcntl.h>
36*1554ba03SSimon J. Gerraty #include <string.h>
37*1554ba03SSimon J. Gerraty #include <syslog.h>
38*1554ba03SSimon J. Gerraty 
39*1554ba03SSimon J. Gerraty #include <security/mac_grantbylabel/mac_grantbylabel.h>
40*1554ba03SSimon J. Gerraty 
41*1554ba03SSimon J. Gerraty #include "libveriexec.h"
42*1554ba03SSimon J. Gerraty 
43*1554ba03SSimon J. Gerraty static char *
find_interpreter(const char * script)44*1554ba03SSimon J. Gerraty find_interpreter(const char *script)
45*1554ba03SSimon J. Gerraty {
46*1554ba03SSimon J. Gerraty 	static const char ws[] = " \t\n\r";
47*1554ba03SSimon J. Gerraty 	static char buf[MAXPATHLEN+4];	/* allow space for #! etc */
48*1554ba03SSimon J. Gerraty 	char *cp;
49*1554ba03SSimon J. Gerraty 	int fd;
50*1554ba03SSimon J. Gerraty 	int n;
51*1554ba03SSimon J. Gerraty 
52*1554ba03SSimon J. Gerraty 	cp = NULL;
53*1554ba03SSimon J. Gerraty 	if ((fd = open(script, O_RDONLY)) >= 0) {
54*1554ba03SSimon J. Gerraty 		if ((n = read(fd, buf, sizeof(buf))) > 0) {
55*1554ba03SSimon J. Gerraty 			if (strncmp(buf, "#!", 2) == 0) {
56*1554ba03SSimon J. Gerraty 				buf[sizeof(buf) - 1] = '\0';
57*1554ba03SSimon J. Gerraty 				cp = &buf[2];
58*1554ba03SSimon J. Gerraty 				if ((n = strspn(cp, ws)) > 0)
59*1554ba03SSimon J. Gerraty 					cp += n;
60*1554ba03SSimon J. Gerraty 				if ((n = strcspn(cp, ws)) > 0) {
61*1554ba03SSimon J. Gerraty 					cp[n] = '\0';
62*1554ba03SSimon J. Gerraty 				} else {
63*1554ba03SSimon J. Gerraty 					cp = NULL;
64*1554ba03SSimon J. Gerraty 				}
65*1554ba03SSimon J. Gerraty 			}
66*1554ba03SSimon J. Gerraty 		}
67*1554ba03SSimon J. Gerraty 		close(fd);
68*1554ba03SSimon J. Gerraty 	}
69*1554ba03SSimon J. Gerraty 	return (cp);
70*1554ba03SSimon J. Gerraty }
71*1554ba03SSimon J. Gerraty 
72*1554ba03SSimon J. Gerraty /**
73*1554ba03SSimon J. Gerraty  * @brief exec a python or similar script
74*1554ba03SSimon J. Gerraty  *
75*1554ba03SSimon J. Gerraty  * Python and similar scripts must normally be signed and
76*1554ba03SSimon J. Gerraty  * run directly rather than fed to the interpreter which
77*1554ba03SSimon J. Gerraty  * is not normally allowed to be run directly.
78*1554ba03SSimon J. Gerraty  *
79*1554ba03SSimon J. Gerraty  * If direct execv of script fails due to EAUTH
80*1554ba03SSimon J. Gerraty  * and process has GBL_VERIEXEC syslog event and run via
81*1554ba03SSimon J. Gerraty  * interpreter.
82*1554ba03SSimon J. Gerraty  *
83*1554ba03SSimon J. Gerraty  * If interpreter is NULL look at first block of script
84*1554ba03SSimon J. Gerraty  * to find ``#!`` magic.
85*1554ba03SSimon J. Gerraty  *
86*1554ba03SSimon J. Gerraty  * @prarm[in] interpreter
87*1554ba03SSimon J. Gerraty  *	if NULL, extract from script if necessary
88*1554ba03SSimon J. Gerraty  *
89*1554ba03SSimon J. Gerraty  * @prarm[in] argv
90*1554ba03SSimon J. Gerraty  *	argv for execv(2)
91*1554ba03SSimon J. Gerraty  *	argv[0] must be full path.
92*1554ba03SSimon J. Gerraty  *	Python at least requires argv[1] to also be the script path.
93*1554ba03SSimon J. Gerraty  *
94*1554ba03SSimon J. Gerraty  * @return
95*1554ba03SSimon J. Gerraty  * error on failure usually EPERM or EAUTH
96*1554ba03SSimon J. Gerraty  */
97*1554ba03SSimon J. Gerraty int
execv_script(const char * interpreter,char * const * argv)98*1554ba03SSimon J. Gerraty execv_script(const char *interpreter, char * const *argv)
99*1554ba03SSimon J. Gerraty {
100*1554ba03SSimon J. Gerraty 	const char *script;
101*1554ba03SSimon J. Gerraty 	int rc;
102*1554ba03SSimon J. Gerraty 
103*1554ba03SSimon J. Gerraty 	script = argv[0];
104*1554ba03SSimon J. Gerraty 	if (veriexec_check_path(script) == 0) {
105*1554ba03SSimon J. Gerraty 		rc = execv(script, argv);
106*1554ba03SSimon J. Gerraty 	}
107*1554ba03SSimon J. Gerraty 	/* still here? we might be allowed to run via interpreter */
108*1554ba03SSimon J. Gerraty 	if (gbl_check_pid(0) & GBL_VERIEXEC) {
109*1554ba03SSimon J. Gerraty 		if (!interpreter)
110*1554ba03SSimon J. Gerraty 			interpreter = find_interpreter(script);
111*1554ba03SSimon J. Gerraty 		if (interpreter) {
112*1554ba03SSimon J. Gerraty 			syslog(LOG_NOTICE, "running %s via %s",
113*1554ba03SSimon J. Gerraty 			    script, interpreter);
114*1554ba03SSimon J. Gerraty 			rc = execv(interpreter, argv);
115*1554ba03SSimon J. Gerraty 		}
116*1554ba03SSimon J. Gerraty 	}
117*1554ba03SSimon J. Gerraty 	return (rc);
118*1554ba03SSimon J. Gerraty }
119*1554ba03SSimon J. Gerraty 
120*1554ba03SSimon J. Gerraty #if defined(MAIN) || defined(UNIT_TEST)
121*1554ba03SSimon J. Gerraty #include <sys/wait.h>
122*1554ba03SSimon J. Gerraty #include <err.h>
123*1554ba03SSimon J. Gerraty 
124*1554ba03SSimon J. Gerraty int
main(int argc __unused,char * argv[])125*1554ba03SSimon J. Gerraty main(int argc __unused, char *argv[])
126*1554ba03SSimon J. Gerraty {
127*1554ba03SSimon J. Gerraty 	const char *interp;
128*1554ba03SSimon J. Gerraty 	int c;
129*1554ba03SSimon J. Gerraty 	int s;
130*1554ba03SSimon J. Gerraty 	pid_t child;
131*1554ba03SSimon J. Gerraty 
132*1554ba03SSimon J. Gerraty 	openlog("exec_script", LOG_PID|LOG_PERROR, LOG_DAEMON);
133*1554ba03SSimon J. Gerraty 
134*1554ba03SSimon J. Gerraty 	interp = NULL;
135*1554ba03SSimon J. Gerraty 	while ((c = getopt(argc, argv, "i:")) != -1) {
136*1554ba03SSimon J. Gerraty 		switch (c) {
137*1554ba03SSimon J. Gerraty 		case 'i':
138*1554ba03SSimon J. Gerraty 			interp = optarg;
139*1554ba03SSimon J. Gerraty 			break;
140*1554ba03SSimon J. Gerraty 		default:
141*1554ba03SSimon J. Gerraty 			errx(1, "unknown option: -%c", c);
142*1554ba03SSimon J. Gerraty 			break;
143*1554ba03SSimon J. Gerraty 		}
144*1554ba03SSimon J. Gerraty 	}
145*1554ba03SSimon J. Gerraty 	argc -= optind;
146*1554ba03SSimon J. Gerraty 	argv += optind;
147*1554ba03SSimon J. Gerraty 	/* we need a child */
148*1554ba03SSimon J. Gerraty 	child = fork();
149*1554ba03SSimon J. Gerraty 	if (child < 0)
150*1554ba03SSimon J. Gerraty 		err(2, "fork");
151*1554ba03SSimon J. Gerraty 	if (child == 0) {
152*1554ba03SSimon J. Gerraty 		c = execv_script(interp, argv);
153*1554ba03SSimon J. Gerraty 		err(2, "exec_script(%s,%s)", interp, argv[0]);
154*1554ba03SSimon J. Gerraty 	}
155*1554ba03SSimon J. Gerraty 	c = waitpid(child, &s, 0);
156*1554ba03SSimon J. Gerraty 	printf("%s: exit %d\n", argv[0], WEXITSTATUS(s));
157*1554ba03SSimon J. Gerraty 	return (0);
158*1554ba03SSimon J. Gerraty }
159*1554ba03SSimon J. Gerraty #endif
160