11554ba03SSimon J. Gerraty /*
21554ba03SSimon J. Gerraty * SPDX-License-Identifier: BSD-2-Clause
31554ba03SSimon J. Gerraty *
41554ba03SSimon J. Gerraty * Copyright (c) 2019-2023, Juniper Networks, Inc.
51554ba03SSimon J. Gerraty * All rights reserved.
61554ba03SSimon J. Gerraty *
71554ba03SSimon J. Gerraty * Redistribution and use in source and binary forms, with or without
81554ba03SSimon J. Gerraty * modification, are permitted provided that the following conditions
91554ba03SSimon J. Gerraty * are met:
101554ba03SSimon J. Gerraty * 1. Redistributions of source code must retain the above copyright
111554ba03SSimon J. Gerraty * notice, this list of conditions and the following disclaimer.
121554ba03SSimon J. Gerraty * 2. Redistributions in binary form must reproduce the above copyright
131554ba03SSimon J. Gerraty * notice, this list of conditions and the following disclaimer in the
141554ba03SSimon J. Gerraty * documentation and/or other materials provided with the distribution.
151554ba03SSimon J. Gerraty *
161554ba03SSimon J. Gerraty * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171554ba03SSimon J. Gerraty * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181554ba03SSimon J. Gerraty * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191554ba03SSimon J. Gerraty * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201554ba03SSimon J. Gerraty * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211554ba03SSimon J. Gerraty * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221554ba03SSimon J. Gerraty * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231554ba03SSimon J. Gerraty * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241554ba03SSimon J. Gerraty * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251554ba03SSimon J. Gerraty * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261554ba03SSimon J. Gerraty * SUCH DAMAGE.
271554ba03SSimon J. Gerraty */
281554ba03SSimon J. Gerraty
291554ba03SSimon J. Gerraty #include <sys/types.h>
301554ba03SSimon J. Gerraty #include <sys/param.h>
311554ba03SSimon J. Gerraty #include <sys/errno.h>
321554ba03SSimon J. Gerraty #include <sys/mac.h>
331554ba03SSimon J. Gerraty
341554ba03SSimon J. Gerraty #include <unistd.h>
351554ba03SSimon J. Gerraty #include <fcntl.h>
361554ba03SSimon J. Gerraty #include <string.h>
371554ba03SSimon J. Gerraty #include <syslog.h>
381554ba03SSimon J. Gerraty
391554ba03SSimon J. Gerraty #include <security/mac_grantbylabel/mac_grantbylabel.h>
401554ba03SSimon J. Gerraty
411554ba03SSimon J. Gerraty #include "libveriexec.h"
421554ba03SSimon J. Gerraty
431554ba03SSimon J. Gerraty static char *
find_interpreter(const char * script)441554ba03SSimon J. Gerraty find_interpreter(const char *script)
451554ba03SSimon J. Gerraty {
461554ba03SSimon J. Gerraty static const char ws[] = " \t\n\r";
471554ba03SSimon J. Gerraty static char buf[MAXPATHLEN+4]; /* allow space for #! etc */
481554ba03SSimon J. Gerraty char *cp;
491554ba03SSimon J. Gerraty int fd;
501554ba03SSimon J. Gerraty int n;
511554ba03SSimon J. Gerraty
521554ba03SSimon J. Gerraty cp = NULL;
531554ba03SSimon J. Gerraty if ((fd = open(script, O_RDONLY)) >= 0) {
541554ba03SSimon J. Gerraty if ((n = read(fd, buf, sizeof(buf))) > 0) {
551554ba03SSimon J. Gerraty if (strncmp(buf, "#!", 2) == 0) {
561554ba03SSimon J. Gerraty buf[sizeof(buf) - 1] = '\0';
571554ba03SSimon J. Gerraty cp = &buf[2];
581554ba03SSimon J. Gerraty if ((n = strspn(cp, ws)) > 0)
591554ba03SSimon J. Gerraty cp += n;
601554ba03SSimon J. Gerraty if ((n = strcspn(cp, ws)) > 0) {
611554ba03SSimon J. Gerraty cp[n] = '\0';
621554ba03SSimon J. Gerraty } else {
631554ba03SSimon J. Gerraty cp = NULL;
641554ba03SSimon J. Gerraty }
651554ba03SSimon J. Gerraty }
661554ba03SSimon J. Gerraty }
671554ba03SSimon J. Gerraty close(fd);
681554ba03SSimon J. Gerraty }
691554ba03SSimon J. Gerraty return (cp);
701554ba03SSimon J. Gerraty }
711554ba03SSimon J. Gerraty
721554ba03SSimon J. Gerraty /**
731554ba03SSimon J. Gerraty * @brief exec a python or similar script
741554ba03SSimon J. Gerraty *
751554ba03SSimon J. Gerraty * Python and similar scripts must normally be signed and
761554ba03SSimon J. Gerraty * run directly rather than fed to the interpreter which
771554ba03SSimon J. Gerraty * is not normally allowed to be run directly.
781554ba03SSimon J. Gerraty *
791554ba03SSimon J. Gerraty * If direct execv of script fails due to EAUTH
801554ba03SSimon J. Gerraty * and process has GBL_VERIEXEC syslog event and run via
811554ba03SSimon J. Gerraty * interpreter.
821554ba03SSimon J. Gerraty *
831554ba03SSimon J. Gerraty * If interpreter is NULL look at first block of script
841554ba03SSimon J. Gerraty * to find ``#!`` magic.
851554ba03SSimon J. Gerraty *
861554ba03SSimon J. Gerraty * @prarm[in] interpreter
871554ba03SSimon J. Gerraty * if NULL, extract from script if necessary
881554ba03SSimon J. Gerraty *
891554ba03SSimon J. Gerraty * @prarm[in] argv
901554ba03SSimon J. Gerraty * argv for execv(2)
911554ba03SSimon J. Gerraty * argv[0] must be full path.
921554ba03SSimon J. Gerraty * Python at least requires argv[1] to also be the script path.
931554ba03SSimon J. Gerraty *
941554ba03SSimon J. Gerraty * @return
951554ba03SSimon J. Gerraty * error on failure usually EPERM or EAUTH
961554ba03SSimon J. Gerraty */
971554ba03SSimon J. Gerraty int
execv_script(const char * interpreter,char * const * argv)981554ba03SSimon J. Gerraty execv_script(const char *interpreter, char * const *argv)
991554ba03SSimon J. Gerraty {
1001554ba03SSimon J. Gerraty const char *script;
1011554ba03SSimon J. Gerraty int rc;
1021554ba03SSimon J. Gerraty
1031554ba03SSimon J. Gerraty script = argv[0];
1041554ba03SSimon J. Gerraty if (veriexec_check_path(script) == 0) {
1051554ba03SSimon J. Gerraty rc = execv(script, argv);
1061554ba03SSimon J. Gerraty }
1071554ba03SSimon J. Gerraty /* still here? we might be allowed to run via interpreter */
1081554ba03SSimon J. Gerraty if (gbl_check_pid(0) & GBL_VERIEXEC) {
1091554ba03SSimon J. Gerraty if (!interpreter)
1101554ba03SSimon J. Gerraty interpreter = find_interpreter(script);
1111554ba03SSimon J. Gerraty if (interpreter) {
1121554ba03SSimon J. Gerraty syslog(LOG_NOTICE, "running %s via %s",
1131554ba03SSimon J. Gerraty script, interpreter);
1141554ba03SSimon J. Gerraty rc = execv(interpreter, argv);
1151554ba03SSimon J. Gerraty }
1161554ba03SSimon J. Gerraty }
1171554ba03SSimon J. Gerraty return (rc);
1181554ba03SSimon J. Gerraty }
1191554ba03SSimon J. Gerraty
1201554ba03SSimon J. Gerraty #if defined(MAIN) || defined(UNIT_TEST)
1211554ba03SSimon J. Gerraty #include <sys/wait.h>
1221554ba03SSimon J. Gerraty #include <err.h>
1231554ba03SSimon J. Gerraty
1241554ba03SSimon J. Gerraty int
main(int argc __unused,char * argv[])1251554ba03SSimon J. Gerraty main(int argc __unused, char *argv[])
1261554ba03SSimon J. Gerraty {
1271554ba03SSimon J. Gerraty const char *interp;
1281554ba03SSimon J. Gerraty int c;
1291554ba03SSimon J. Gerraty int s;
1301554ba03SSimon J. Gerraty pid_t child;
1311554ba03SSimon J. Gerraty
1321554ba03SSimon J. Gerraty openlog("exec_script", LOG_PID|LOG_PERROR, LOG_DAEMON);
1331554ba03SSimon J. Gerraty
1341554ba03SSimon J. Gerraty interp = NULL;
1351554ba03SSimon J. Gerraty while ((c = getopt(argc, argv, "i:")) != -1) {
1361554ba03SSimon J. Gerraty switch (c) {
1371554ba03SSimon J. Gerraty case 'i':
1381554ba03SSimon J. Gerraty interp = optarg;
1391554ba03SSimon J. Gerraty break;
1401554ba03SSimon J. Gerraty default:
1411554ba03SSimon J. Gerraty errx(1, "unknown option: -%c", c);
1421554ba03SSimon J. Gerraty break;
1431554ba03SSimon J. Gerraty }
1441554ba03SSimon J. Gerraty }
1451554ba03SSimon J. Gerraty argc -= optind;
1461554ba03SSimon J. Gerraty argv += optind;
1471554ba03SSimon J. Gerraty /* we need a child */
1481554ba03SSimon J. Gerraty child = fork();
1491554ba03SSimon J. Gerraty if (child < 0)
1501554ba03SSimon J. Gerraty err(2, "fork");
1511554ba03SSimon J. Gerraty if (child == 0) {
1521554ba03SSimon J. Gerraty c = execv_script(interp, argv);
1531554ba03SSimon J. Gerraty err(2, "exec_script(%s,%s)", interp, argv[0]);
1541554ba03SSimon J. Gerraty }
1551554ba03SSimon J. Gerraty c = waitpid(child, &s, 0);
1561554ba03SSimon J. Gerraty printf("%s: exit %d\n", argv[0], WEXITSTATUS(s));
1571554ba03SSimon J. Gerraty return (0);
1581554ba03SSimon J. Gerraty }
1591554ba03SSimon J. Gerraty #endif
160