1*41b03932SMark Johnston /*
2*41b03932SMark Johnston * Copyright (c) 2026 Mark Johnston <markj@FreeBSD.org>
3*41b03932SMark Johnston *
4*41b03932SMark Johnston * SPDX-License-Identifier: BSD-2-Clause
5*41b03932SMark Johnston */
6*41b03932SMark Johnston
7*41b03932SMark Johnston /*
8*41b03932SMark Johnston * A minimal regression test for FreeBSD-SA-26:13.exec.
9*41b03932SMark Johnston */
10*41b03932SMark Johnston
11*41b03932SMark Johnston #include <err.h>
12*41b03932SMark Johnston #include <fcntl.h>
13*41b03932SMark Johnston #include <limits.h>
14*41b03932SMark Johnston #include <stdlib.h>
15*41b03932SMark Johnston #include <string.h>
16*41b03932SMark Johnston #include <unistd.h>
17*41b03932SMark Johnston
18*41b03932SMark Johnston #define SCRIPTNAME "script"
19*41b03932SMark Johnston #define SCRIPTBODY "#!/bin/sh\nexit 0\n"
20*41b03932SMark Johnston
21*41b03932SMark Johnston int
main(void)22*41b03932SMark Johnston main(void)
23*41b03932SMark Johnston {
24*41b03932SMark Johnston char *argv;
25*41b03932SMark Johnston size_t size;
26*41b03932SMark Johnston int fd;
27*41b03932SMark Johnston
28*41b03932SMark Johnston fd = open(SCRIPTNAME, O_WRONLY | O_CREAT | O_TRUNC, 0700);
29*41b03932SMark Johnston if (fd == -1)
30*41b03932SMark Johnston err(1, "open");
31*41b03932SMark Johnston if (write(fd, SCRIPTBODY, sizeof(SCRIPTBODY) - 1) !=
32*41b03932SMark Johnston sizeof(SCRIPTBODY) - 1)
33*41b03932SMark Johnston err(1, "write");
34*41b03932SMark Johnston close(fd);
35*41b03932SMark Johnston
36*41b03932SMark Johnston size = ARG_MAX / 2;
37*41b03932SMark Johnston argv = malloc(size);
38*41b03932SMark Johnston if (argv == NULL)
39*41b03932SMark Johnston err(1, "malloc");
40*41b03932SMark Johnston memset(argv, 'a', size - 1);
41*41b03932SMark Johnston argv[size - 1] = '\0';
42*41b03932SMark Johnston
43*41b03932SMark Johnston execve(SCRIPTNAME, (char *[]){ argv, NULL }, (char *[]){ NULL });
44*41b03932SMark Johnston
45*41b03932SMark Johnston exit(1);
46*41b03932SMark Johnston }
47