summaryrefslogtreecommitdiff
path: root/regress/sys/kern/setuid/setgid.c
diff options
context:
space:
mode:
Diffstat (limited to 'regress/sys/kern/setuid/setgid.c')
-rw-r--r--regress/sys/kern/setuid/setgid.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/regress/sys/kern/setuid/setgid.c b/regress/sys/kern/setuid/setgid.c
new file mode 100644
index 00000000000..ee2b04a5cf1
--- /dev/null
+++ b/regress/sys/kern/setuid/setgid.c
@@ -0,0 +1,52 @@
+/* $OpenBSD: setgid.c,v 1.1 2014/08/27 07:36:14 blambert Exp $ */
+/*
+ * Written by Bret Stephen Lambert <blambert@openbsd.org> 2014
+ * Public Domain.
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pwd.h>
+#include <unistd.h>
+
+#include "setuid_regress.h"
+
+int
+main(int argc, const char *argv[])
+{
+ struct kinfo_proc kproc;
+ struct passwd *pw;
+
+ if ((pw = getpwnam(_SETUID_REGRESS_USER)) == NULL)
+ err(1, "unknown user \"%s\"", _SETUID_REGRESS_USER);
+
+ /*
+ * From the setgid man page:
+ * The setgid() function sets the real and effective group IDs
+ * and the saved set-group-ID of the current process
+ */
+ if (setgid(pw->pw_gid) == -1)
+ err(1, "setgid");
+ checkgids(pw->pw_gid, pw->pw_gid, pw->pw_gid, "setgid");
+
+ /* should only respond to setuid upon exec */
+ if (issetugid())
+ errx(1, "process incorrectly marked as issetugid()");
+
+ if (read_kproc_pid(&kproc, getpid()) == -1)
+ err(1, "kproc read failed");
+
+ if (!(kproc.p_psflags & PS_SUGID))
+ errx(1, "PS_SUGID not set");
+ if (kproc.p_psflags & PS_SUGIDEXEC)
+ errx(1, "PS_SUGIDEXEC incorrectly set");
+
+ exit(0);
+}