Last active
April 29, 2017 14:43
-
-
Save austinkeeley/de7bc44b84caef9edd7cb587d3ba2c80 to your computer and use it in GitHub Desktop.
Setuid wrapper and Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makefile for the wrapper | |
# The wrapper target is going to prompt you for your root password | |
# so don't run this if you are set to do stuff non-interactively. | |
THIS_DIR = $(shell pwd) | |
DEFINES=-DPING_AGENT_PATH="\"$(THIS_DIR)\"" | |
wrapper: wrapper.o | |
gcc -o wrapper wrapper.c $(DEFINES) | |
sudo chown root wrapper | |
sudo chmod u+s wrapper | |
%.o: %.c | |
gcc -c $(DEFINES) $< | |
clean: | |
rm -f wrapper | |
rm -f *.o | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* wrapper.c | |
* Wrapper around the ping-agent.js. | |
* It is stupid dangerous to run setuid so this requires you to | |
* set the explicit path at compile time with a -D flag. | |
*/ | |
#ifndef PING_AGENT_PATH | |
#error you forgot to set the PING_AGENT_PATH | |
#endif | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
const char *PATH = PING_AGENT_PATH "/ping-agent.js"; | |
char *ARGS[] = {"ping-agent.js"}; | |
int main(int argc, char *argv) { | |
printf("Ping agent wrapper\n"); | |
printf("Path: %s\n", PATH); | |
// Are we running this with setuid? If not, we probably | |
// messed something up. | |
uid_t real_uid = getuid(); | |
uid_t effective_uid = geteuid(); | |
printf("Real UID: %d\n", real_uid); | |
printf("Effective UID: %d\n", effective_uid); | |
if (0 != effective_uid) { | |
fprintf(stderr, "WARNING: Not running as root\n"); | |
} | |
execv(PATH, ARGS); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment