xref: /openbsd/regress/sys/kern/setuid/sgidexec.c (revision 49a6e16f)
1 /*	$OpenBSD: sgidexec.c,v 1.2 2021/12/13 16:56:50 deraadt 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/proc.h>
9 #include <sys/sysctl.h>
10 #include <sys/wait.h>
11 
12 #include <err.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <pwd.h>
17 #include <unistd.h>
18 
19 #include "setuid_regress.h"
20 
21 int
22 main(int argc, char *argv[])
23 {
24 	struct kinfo_proc	 kproc;
25 	char			*toexec = NULL;
26 
27 	if (argc > 1) {
28 		argv++;
29 		if ((toexec = strdup(argv[0])) == NULL)
30 			err(1, "strdup");
31 	}
32 
33 	if (read_kproc_pid(&kproc, getpid()) == -1)
34 		err(1, "kproc read failed");
35 
36 	if (issetugid() != 1)
37 		errx(1, "process not marked as issetugid()");
38 
39 	if (!(kproc.p_psflags & PS_SUGID))
40 		errx(1, "PS_SUGID not set");
41 	if (!(kproc.p_psflags & PS_SUGIDEXEC))
42 		errx(1, "PS_SUGIDEXEC not set");
43 
44 	if (toexec != NULL)
45 		if (execv(toexec, argv) == -1)
46 			err(1, "exec of %s failed", toexec);
47 	free(toexec);
48 
49 	exit(0);
50 }
51