1 /*	$OpenBSD: sgidexec_inherit.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 	if (!issetugid())
35 		errx(1, "process not marked as issetugid()");
36 
37 	if (read_kproc_pid(&kproc, getpid()) == -1)
38 		err(1, "kproc read failed");
39 
40 	if (kproc.p_psflags & PS_SUGID)
41 		errx(1, "PS_SUGID incorrectly set");
42 	if (!(kproc.p_psflags & PS_SUGIDEXEC))
43 		errx(1, "PS_SUGIDEXEC not set");
44 
45 	if (toexec != NULL)
46 		if (execv(toexec, argv) == -1)
47 			err(1, "exec of %s failed", toexec);
48 	free(toexec);
49 
50 	exit(0);
51 }
52