From 68d0d0b7590831962e9272987ba826551c5decce Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 28 Oct 2022 17:52:46 +0200 Subject: [PATCH] apps: Add a new su utility --- apps/Makefile | 2 +- apps/src/su.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 apps/src/su.c diff --git a/apps/Makefile b/apps/Makefile index 709f0cd2..d08aa8b2 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,4 +1,4 @@ -APPS := init sym sh crash uname uptime hello ps ls args cat stat +APPS := init sym sh crash uname uptime hello ps ls args cat stat su APPS_DIR := $(LUNA_ROOT)/apps APPS_SRC := $(APPS_DIR)/src diff --git a/apps/src/su.c b/apps/src/su.c new file mode 100644 index 00000000..0c11c520 --- /dev/null +++ b/apps/src/su.c @@ -0,0 +1,32 @@ +#include +#include +#include + +char* default_argv[] = {"/bin/sh", NULL}; + +void run_program(char** argv) +{ + execv(argv[0], argv); + + perror("execv"); + exit(EXIT_FAILURE); +} + +int main(int argc, char** argv) +{ + if (argc == 1) + { + fprintf(stderr, "Usage: %s [uid] [command | sh]\n", argv[0]); + return EXIT_FAILURE; + } + + if (setuid(atoi(argv[1])) < 0) + { + perror("setuid"); + return EXIT_FAILURE; + } + + if (argc == 2) run_program(default_argv); + else + run_program(argv + 2); +} \ No newline at end of file