1 /*	$OpenBSD: setresuid_saved_exec.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 	struct passwd		*pw;
27 	char			*toexec = NULL;
28 	uid_t			 uid;
29 
30 	if (argc > 1) {
31 		argv ++;
32 		if ((toexec = strdup(argv[0])) == NULL)
33 			err(1, "strdup");
34 	}
35 
36 	if ((pw = getpwnam(_SETUID_REGRESS_USER)) == NULL)
37 		err(1, "unknown user \"%s\"", _SETUID_REGRESS_USER);
38 
39 	uid = getuid();
40 
41 	if (setresuid(-1, -1, pw->pw_uid) == -1)
42 		err(1, "setuid");
43 	checkuids(uid, uid, pw->pw_uid, "setuid");
44 
45 	/* should only respond to setuid upon exec */
46 	if (issetugid())
47 		errx(1, "process incorrectly as issetugid()");
48 
49 	if (read_kproc_pid(&kproc, getpid()) == -1)
50 		err(1, "kproc read failed");
51 
52 	if (!(kproc.p_psflags & PS_SUGID))
53 		errx(1, "PS_SUGID not set");
54 	if (kproc.p_psflags & PS_SUGIDEXEC)
55 		errx(1, "PS_SUGIDEXEC incorrectly set");
56 
57 	if (toexec != NULL)
58 		if (execv(toexec, argv) == -1)
59 			err(1, "exec of %s failed", toexec);
60 	free(toexec);
61 
62 	exit(0);
63 }
64