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