From b191d87e338ac11c373f4553a2ba3ccd47e6c998 Mon Sep 17 00:00:00 2001 From: Daniel Richard G Date: Sat, 13 May 2023 02:06:26 -0400 Subject: [PATCH] Move Linux's no_new_privs call into os_calls This helps keep the application code free of platform-specific cruft. Also remove a needless #include from sesman/session_list.c. --- common/os_calls.c | 19 +++++++++++++++++++ common/os_calls.h | 1 + sesman/sesexec/session.c | 13 +------------ sesman/session_list.c | 4 ---- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/common/os_calls.c b/common/os_calls.c index 8f4ec707..53dcf4a1 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -53,6 +53,9 @@ #include #include #include +#if defined(HAVE_SYS_PRCTL_H) +#include +#endif #include #include #include @@ -3954,3 +3957,19 @@ g_tcp6_bind_address(int sck, const char *port, const char *address) return -1; #endif } + +/*****************************************************************************/ +/* returns error, zero is success, non zero is error */ +/* only works in linux */ +int +g_no_new_privs(void) +{ +#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NO_NEW_PRIVS) + /* + * PR_SET_NO_NEW_PRIVS requires Linux kernel 3.5 and newer. + */ + return prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); +#else + return 0; +#endif +} diff --git a/common/os_calls.h b/common/os_calls.h index dfbc85cb..8e095d32 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -333,6 +333,7 @@ int g_tcp4_socket(void); int g_tcp4_bind_address(int sck, const char *port, const char *address); int g_tcp6_socket(void); int g_tcp6_bind_address(int sck, const char *port, const char *address); +int g_no_new_privs(void); /* glib-style wrappers */ #define g_new(struct_type, n_structs) \ diff --git a/sesman/sesexec/session.c b/sesman/sesexec/session.c index d9a0c54c..6d45e994 100644 --- a/sesman/sesexec/session.c +++ b/sesman/sesexec/session.c @@ -33,10 +33,6 @@ #include "config_ac.h" #endif -#ifdef HAVE_SYS_PRCTL_H -#include -#endif - #include #include "arch.h" @@ -56,10 +52,6 @@ #include "xwait.h" #include "xrdp_sockets.h" -#ifndef PR_SET_NO_NEW_PRIVS -#define PR_SET_NO_NEW_PRIVS 38 -#endif - struct session_data { pid_t x_server; ///< PID of X server @@ -347,21 +339,18 @@ prepare_xorg_xserver_params(const struct session_parameters *s, { params->auto_free = 1; -#ifdef HAVE_SYS_PRCTL_H /* * Make sure Xorg doesn't run setuid root. Root access is not * needed. Xorg can fail when run as root and the user has no * console permissions. - * PR_SET_NO_NEW_PRIVS requires Linux kernel 3.5 and newer. */ - if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) + if (g_no_new_privs() != 0) { LOG(LOG_LEVEL_WARNING, "[session start] (display %u): Failed to disable " "setuid on X server: %s", s->display, g_get_strerror()); } -#endif g_snprintf(screen, sizeof(screen), ":%u", s->display); diff --git a/sesman/session_list.c b/sesman/session_list.c index 36a6c9f9..497c6eb8 100644 --- a/sesman/session_list.c +++ b/sesman/session_list.c @@ -33,10 +33,6 @@ #include "config_ac.h" #endif -#ifdef HAVE_SYS_PRCTL_H -#include -#endif - #include "arch.h" #include "session_list.h" #include "trans.h"