From 2e08db36952ab6c55eff0b0a70a5bfed06edbf5f Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Sun, 9 Jan 2022 17:31:06 -0700 Subject: [PATCH] If sudo is not set-user-ID root, check for the no_new_privs flag on Linux. This flag disables set-user-ID at execve(2) time and may be set by default for some containers. GitHub issue #129. --- src/sudo.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/sudo.c b/src/sudo.c index f08e47981..85911a307 100644 --- a/src/sudo.c +++ b/src/sudo.c @@ -32,6 +32,9 @@ #include #include #include +#ifdef __linux__ +# include +#endif #include #include #include @@ -318,7 +321,7 @@ main(int argc, char *argv[], char *envp[]) sa.sa_handler = SIG_DFL; sigaction(WTERMSIG(status), &sa, NULL); sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, - WTERMSIG(status) | 128); + WTERMSIG(status) | 128); kill(getpid(), WTERMSIG(status)); } sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, @@ -899,6 +902,17 @@ sudo_check_suid(const char *sudo) debug_decl(sudo_check_suid, SUDO_DEBUG_PCOMM); if (geteuid() != ROOT_UID) { +#if defined(__linux__) && defined(PR_GET_NO_NEW_PRIVS) + /* The no_new_privs flag disables set-user-ID at execve(2) time. */ + if (prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) == 1) { + sudo_warnx(U_("The \"no new privileges\" flag is set, which " + "prevents sudo from running as root.")); + sudo_warnx(U_("If sudo is running in a container, you may need " + "to adjust the container configuration to disable the flag.")); + exit(EXIT_FAILURE); + } +#endif /* __linux__ && PR_GET_NO_NEW_PRIVS */ + /* Search for sudo binary in PATH if not fully qualified. */ qualified = strchr(sudo, '/') != NULL; if (!qualified) {