From e811fc0be503a4ceaa0c4ed5f9267df5e1d345f6 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 6 Dec 2023 18:15:42 +0200 Subject: [PATCH] Userspace: Implement barebones sudo This doesn't do any password checking or anything. Just sets uid and gid before execvp() --- userspace/CMakeLists.txt | 1 + userspace/sudo/CMakeLists.txt | 17 +++++++++++++++++ userspace/sudo/main.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 userspace/sudo/CMakeLists.txt create mode 100644 userspace/sudo/main.cpp diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt index a53eac04..1c36198b 100644 --- a/userspace/CMakeLists.txt +++ b/userspace/CMakeLists.txt @@ -22,6 +22,7 @@ set(USERSPACE_PROJECTS sleep snake stat + sudo sync tee test diff --git a/userspace/sudo/CMakeLists.txt b/userspace/sudo/CMakeLists.txt new file mode 100644 index 00000000..f1cf6b29 --- /dev/null +++ b/userspace/sudo/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.26) + +project(sudo CXX) + +set(SOURCES + main.cpp +) + +add_executable(sudo ${SOURCES}) +target_compile_options(sudo PUBLIC -O2 -g) +target_link_libraries(sudo PUBLIC libc) + +add_custom_target(sudo-install + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/sudo ${BANAN_BIN}/ + COMMAND /bin/chmod u+s ${BANAN_BIN}/sudo + DEPENDS sudo +) diff --git a/userspace/sudo/main.cpp b/userspace/sudo/main.cpp new file mode 100644 index 00000000..3ab61075 --- /dev/null +++ b/userspace/sudo/main.cpp @@ -0,0 +1,31 @@ +#include +#include + +int usage(char* argv0, int ret) +{ + FILE* fp = (ret == 0) ? stdout : stderr; + fprintf(fp, "usage: %s COMMAND [ARGUMENTS]\n", argv0); + return ret; +} + +int main(int argc, char** argv) +{ + if (argc < 2) + return usage(argv[0], 1); + + if (setuid(0) == -1) + { + perror("setuid"); + return 1; + } + if (setgid(0) == -1) + { + perror("setgid"); + return 1; + } + + execvp(argv[1], argv + 1); + + perror("execv"); + return 1; +}