#include #include #include #include #include #include #include #include #include #include int delete_mod = 0; static int run_as(uid_t uid, gid_t gid, const char *path, char *const argv[]) { int i = 0; fprintf(stderr, "execing: "); while (argv[i]) { fprintf(stderr, "%s ", argv[i]); i++; } fprintf(stderr, "\n"); /* Make sure we run as the full user. If we're * switching to a non-root user, this won't allow * that process to switch back to root (since the * original process is setuid). */ if (setresgid (gid, gid, gid) < 0) { perror("setresgid"); exit(1); } if (setresuid (uid, uid, uid) < 0) { perror("setresuid"); exit(1); } /* Actually run the command. */ if (execv(path, argv) < 0) perror(path); _exit(1); return -1; } int main(int argc, char **argv) { char *prog = argv[0]; char *newargv[2]; if (argc > 1) delete_mod = 1; fprintf(stderr, "%s: uid = %d, euid = %d\n", prog, (int)getuid(), (int)geteuid()); if (geteuid() != 0) { fprintf(stderr, "%s: ERROR - The effective user ID isn't root!\n", prog); exit(1); } if (delete_mod) exit(0); newargv[0] = "./test2"; newargv[1] = NULL; if (run_as (getuid(), getgid(), newargv[0], newargv) < 0) { perror(argv[0]); return 1; } return 0; }