diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 017e0a8539e..01fe7e50ae0 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -726,6 +726,17 @@ ErrorOr setsid() return rc; } +ErrorOr drop_privileges() +{ + auto gid_result = setgid(getgid()); + auto uid_result = setuid(getuid()); + + if (gid_result.is_error() || uid_result.is_error()) + return Error::from_string_literal("Failed to drop privileges"); + + return {}; +} + ErrorOr isatty(int fd) { int rc = ::isatty(fd); diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 1ac96cf76c2..b8bf14eae4b 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -109,6 +109,7 @@ ErrorOr setgid(gid_t); ErrorOr setegid(gid_t); ErrorOr setpgid(pid_t pid, pid_t pgid); ErrorOr setsid(); +ErrorOr drop_privileges(); ErrorOr isatty(int fd); ErrorOr symlink(StringView target, StringView link_path); ErrorOr mkdir(StringView path, mode_t);