1 /*	$OpenBSD: sgidexec_none.c,v 1.3 2021/12/15 18:42:38 anton Exp $	*/
2 /*
3  *	Written by Bret Stephen Lambert <blambert@openbsd.org> 2014
4  *	Public Domain.
5  */
6 
7 #include <sys/types.h>
8 #include <sys/signal.h>
9 #include <sys/proc.h>
10 #include <sys/sysctl.h>
11 #include <sys/wait.h>
12 
13 #include <err.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <pwd.h>
18 #include <unistd.h>
19 
20 #include "setuid_regress.h"
21 
22 int
23 main(int argc, char *argv[])
24 {
25 	struct kinfo_proc	 kproc;
26 	char			*toexec = NULL;
27 
28 	if (argc > 1) {
29 		argv++;
30 		if ((toexec = strdup(argv[0])) == NULL)
31 			err(1, "strdup");
32 	}
33 
34 	/* should only respond to setgid upon exec */
35 	if (issetugid())
36 		errx(1, "process incorrectly marked as issetugid()");
37 
38 	if (read_kproc_pid(&kproc, getpid()) == -1)
39 		err(1, "kproc read failed");
40 
41 	if (kproc.p_psflags & PS_SUGID)
42 		errx(1, "PS_SUGID not set");
43 	if (kproc.p_psflags & PS_SUGIDEXEC)
44 		errx(1, "PS_SUGIDEXEC incorrectly set");
45 
46 	if (toexec != NULL)
47 		if (execv(toexec, argv) == -1)
48 			err(1, "exec of %s failed", toexec);
49 	free(toexec);
50 
51 	exit(0);
52 }
53