21 uid_t uid
, euid
, suid
;
22 gid_t gid
, egid
, sgid
;
24 getresuid(&uid
, &euid
, &suid
);
25 getresgid(&gid
, &egid
, &sgid
);
27 outputstd("initial uid:%d gid:%d euid:%d egid:%d suid:%d sgid:%d\n",
28 uid
, gid
, euid
, egid
, suid
, sgid
);
33 if (setresgid(nobody_gid
, nobody_gid
, nobody_gid
) < 0) {
34 outputerr("Error setting nobody gid (%s)\n", strerror(errno
));
38 if (setgroups(0, NULL
) == -1) {
39 outputerr("Error dropping supplemental groups (%s)\n", strerror(errno
));
43 if (setresuid(nobody_uid
, nobody_uid
, nobody_uid
) < 0) {
44 outputerr("Error setting nobody uid (%s)\n", strerror(errno
));
48 outputstd("set uid to %d and gid to %d (nobody)\n", nobody_uid
, nobody_gid
);
53 struct passwd
*passwd
;
58 if (dropprivs
== FALSE
)
61 passwd
= getpwnam("nobody");
63 outputerr("Error getting nobody pwent (%s)\n", strerror(errno
));
66 nobody_uid
= passwd
->pw_uid
;
67 nobody_gid
= passwd
->pw_gid
;
71 void do_uid0_check(void)
75 /* if we're already unprivileged, then don't worry. */
79 if (dangerous
== TRUE
) {
80 outputstd("DANGER: RUNNING AS ROOT.\n");
81 outputstd("Unless you are running in a virtual machine, this could cause serious problems such as overwriting CMOS\n");
82 outputstd("or similar which could potentially make this machine unbootable without a firmware reset.\n");
83 outputstd("You might want to check out running with --dropprivs (currently experimental).\n\n");
86 if (dropprivs
== FALSE
) {
87 outputstd("Don't run as root (or pass --dangerous, or --dropprivs if you know what you are doing).\n");
90 outputstd("--dropprivs is still in development, and really shouldn't be used unless you're helping development. Expect crashes.\n");
91 outputstd("Going to run as user nobody (uid:%d gid:%d)\n", nobody_uid
, nobody_gid
);
95 outputstd("ctrl-c now unless you really know what you are doing.\n");
96 for (i
= 10; i
> 0; i
--) {
97 outputstd("Continuing in %d seconds.\r", i
);
107 /* If we were root, then obviously setuid() will change us, so don't even check. */
113 /* we should be 'nobody' if we ran with --dropprivs */
114 if (dropprivs
== TRUE
) {
115 if (myuid
== nobody_uid
)
121 if (myuid
!= orig_uid
) {
124 /* unshare() can change us to /proc/sys/kernel/overflowuid */
128 output(0, "uid changed! Was: %d, now %d\n", orig_uid
, myuid
);
130 shm
->exit_reason
= EXIT_UID_CHANGED
;