1 /* $OpenBSD: setgid.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 <pwd.h>
17 #include <unistd.h>
18
19 #include "setuid_regress.h"
20
21 int
main(int argc,const char * argv[])22 main(int argc, const char *argv[])
23 {
24 struct kinfo_proc kproc;
25 struct passwd *pw;
26
27 if ((pw = getpwnam(_SETUID_REGRESS_USER)) == NULL)
28 err(1, "unknown user \"%s\"", _SETUID_REGRESS_USER);
29
30 /*
31 * From the setgid man page:
32 * The setgid() function sets the real and effective group IDs
33 * and the saved set-group-ID of the current process
34 */
35 if (setgid(pw->pw_gid) == -1)
36 err(1, "setgid");
37 checkgids(pw->pw_gid, pw->pw_gid, pw->pw_gid, "setgid");
38
39 /* should only respond to setuid upon exec */
40 if (issetugid())
41 errx(1, "process incorrectly marked as issetugid()");
42
43 if (read_kproc_pid(&kproc, getpid()) == -1)
44 err(1, "kproc read failed");
45
46 if (!(kproc.p_psflags & PS_SUGID))
47 errx(1, "PS_SUGID not set");
48 if (kproc.p_psflags & PS_SUGIDEXEC)
49 errx(1, "PS_SUGIDEXEC incorrectly set");
50
51 exit(0);
52 }
53