From 85be139a7d4b718192cd68e3d3dcff49a9384c2d Mon Sep 17 00:00:00 2001 From: Takashi Sato Date: Mon, 11 Aug 2014 03:04:21 +0000 Subject: [PATCH 1/5] unixd_drop_privileges and ap_unixd_setup_child are almost the same, so let's remove the redundant code. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1617196 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit 4e5e8a3b0bb4fd71ea338b8499c839ac2d146206) --- modules/arch/unix/mod_unixd.c | 58 +++-------------------------------- 1 file changed, 5 insertions(+), 53 deletions(-) diff --git a/modules/arch/unix/mod_unixd.c b/modules/arch/unix/mod_unixd.c index 1baa278c3fd..10b9b170348 100644 --- a/modules/arch/unix/mod_unixd.c +++ b/modules/arch/unix/mod_unixd.c @@ -134,9 +134,13 @@ static int set_group_privs(void) return 0; } - static int unixd_drop_privileges(apr_pool_t *pool, server_rec *s) +{ + return ap_unixd_setup_child(); +} + +AP_DECLARE(int) ap_unixd_setup_child(void) { int rv = set_group_privs(); @@ -326,58 +330,6 @@ unixd_pre_config(apr_pool_t *pconf, apr_pool_t *plog, return OK; } -AP_DECLARE(int) ap_unixd_setup_child(void) -{ - if (set_group_privs()) { - return -1; - } - - if (NULL != ap_unixd_config.chroot_dir) { - if (geteuid()) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(02164) - "Cannot chroot when not started as root"); - return -1; - } - if (chdir(ap_unixd_config.chroot_dir) != 0) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(02165) - "Can't chdir to %s", ap_unixd_config.chroot_dir); - return -1; - } - if (chroot(ap_unixd_config.chroot_dir) != 0) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(02166) - "Can't chroot to %s", ap_unixd_config.chroot_dir); - return -1; - } - if (chdir("/") != 0) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(02167) - "Can't chdir to new root"); - return -1; - } - } - - /* Only try to switch if we're running as root */ - if (!geteuid() && ( -#ifdef _OSD_POSIX - os_init_job_environment(NULL, ap_unixd_config.user_name, ap_exists_config_define("DEBUG")) != 0 || -#endif - setuid(ap_unixd_config.user_id) == -1)) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(02168) - "setuid: unable to change to uid: %ld", - (long) ap_unixd_config.user_id); - return -1; - } -#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) - /* this applies to Linux 2.4+ */ - if (ap_coredumpdir_configured) { - if (prctl(PR_SET_DUMPABLE, 1)) { - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(02169) - "set dumpable failed - this child will not coredump" - " after software errors"); - } - } -#endif - return 0; -} static void unixd_dump_config(apr_pool_t *p, server_rec *s) { From 07ad1d1fe2a9a2588044974a87ddb2a1a73b4d9a Mon Sep 17 00:00:00 2001 From: Takashi Sato Date: Mon, 11 Aug 2014 05:06:50 +0000 Subject: [PATCH 2/5] geteuid is always successful, so remove errno reference. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1617201 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit cd503763ee564c30ab9301dcd94ddf09011a88e6) --- modules/arch/unix/mod_unixd.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/arch/unix/mod_unixd.c b/modules/arch/unix/mod_unixd.c index 10b9b170348..bb056c01c08 100644 --- a/modules/arch/unix/mod_unixd.c +++ b/modules/arch/unix/mod_unixd.c @@ -150,8 +150,7 @@ AP_DECLARE(int) ap_unixd_setup_child(void) if (NULL != ap_unixd_config.chroot_dir) { if (geteuid()) { - rv = errno; - ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(02158) + ap_log_error(APLOG_MARK, APLOG_ALERT, 0, NULL, APLOGNO(02158) "Cannot chroot when not started as root"); return rv; } From 08cc879a7b327394a4e9d1a498c4e106f866c23a Mon Sep 17 00:00:00 2001 From: Takashi Sato Date: Tue, 19 Aug 2014 01:27:38 +0000 Subject: [PATCH 3/5] Follow up r1617201: Return EPERM if the uid is not root on chroot-ing. Pointed out by trawick on git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1618778 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit 89b58e85526bb7580b5df6da67a9894f118bd910) --- modules/arch/unix/mod_unixd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/arch/unix/mod_unixd.c b/modules/arch/unix/mod_unixd.c index bb056c01c08..09343d368e2 100644 --- a/modules/arch/unix/mod_unixd.c +++ b/modules/arch/unix/mod_unixd.c @@ -152,7 +152,7 @@ AP_DECLARE(int) ap_unixd_setup_child(void) if (geteuid()) { ap_log_error(APLOG_MARK, APLOG_ALERT, 0, NULL, APLOGNO(02158) "Cannot chroot when not started as root"); - return rv; + return EPERM; } if (chdir(ap_unixd_config.chroot_dir) != 0) { From b759ee14692c26b5ab1c37a1ed695a8132b0c21f Mon Sep 17 00:00:00 2001 From: Yann Ylavic Date: Thu, 20 Jan 2022 20:03:04 +0000 Subject: [PATCH 4/5] mod_unixd: Make CoreDumpDirectory work for FreeBSD 11+. PR 65819. FreeBSD 11+ coredumping requires tracing enabled via procctl(PROC_TRACE_CTL). Submitted by: David CARLIER Reviewed by: ylavic (by inspection) git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1897269 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit af558302645384b998333f214d0af37cb3a4ff26) --- changes-entries/CoreDumpDirectory-freebsd11.txt | 2 ++ configure.in | 2 ++ modules/arch/unix/mod_unixd.c | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 changes-entries/CoreDumpDirectory-freebsd11.txt diff --git a/changes-entries/CoreDumpDirectory-freebsd11.txt b/changes-entries/CoreDumpDirectory-freebsd11.txt new file mode 100644 index 00000000000..e200f0cc027 --- /dev/null +++ b/changes-entries/CoreDumpDirectory-freebsd11.txt @@ -0,0 +1,2 @@ + *) mod_unixd: CoreDumpDirectory requires enabling tracing on FreeBSD 11+. + PR 65819. [David CARLIER ] diff --git a/configure.in b/configure.in index d2a009d7902..a4a9dba9c6b 100644 --- a/configure.in +++ b/configure.in @@ -463,6 +463,7 @@ pwd.h \ grp.h \ strings.h \ sys/prctl.h \ +sys/procctl.h \ sys/processor.h \ sys/sem.h \ sys/sdt.h \ @@ -520,6 +521,7 @@ getgrnam \ initgroups \ bindprocessor \ prctl \ +procctl \ timegm \ getpgid \ fopen64 \ diff --git a/modules/arch/unix/mod_unixd.c b/modules/arch/unix/mod_unixd.c index 09343d368e2..4ce279856f9 100644 --- a/modules/arch/unix/mod_unixd.c +++ b/modules/arch/unix/mod_unixd.c @@ -50,6 +50,9 @@ #ifdef HAVE_SYS_PRCTL_H #include #endif +#ifdef HAVE_SYS_PROCCTL_H +#include +#endif #ifndef DEFAULT_USER #define DEFAULT_USER "#-1" @@ -201,6 +204,19 @@ AP_DECLARE(int) ap_unixd_setup_child(void) } } #endif +#if defined(HAVE_PROCCTL) && defined(PROC_TRACE_CTL) + /* FreeBSD 11 and above */ + if (ap_coredumpdir_configured) { + int enablecoredump = PROC_TRACE_CTL_ENABLE; + if (procctl(P_PID, 0, PROC_TRACE_CTL, &enablecoredump) != 0) { + rv = errno; + ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(10369) + "set dumpable failed - this child will not coredump" + " after software errors"); + return rv; + } + } +#endif return OK; } From 341a784bf633bbf06cdf498eb751092eeebff4d4 Mon Sep 17 00:00:00 2001 From: Joe Orton Date: Fri, 15 Aug 2025 07:38:26 +0000 Subject: [PATCH 5/5] * modules/arch/unix/mod_unixd.ci (ap_unixd_setup_child): Do not test euid=0 before going chroot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nowaday chroot need CAP_SYS_CHROOT capability in its user namespace, and could work without root. Will allow to use chroot with lesser permission. Submitted by: Bastien Roucariès PR: 69767 Github: closes #549 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1927804 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit 5c9c78d7859f6a45e8267f7017313002316c3257) --- changes-entries/pr69767.txt | 3 +++ modules/arch/unix/mod_unixd.c | 6 ------ 2 files changed, 3 insertions(+), 6 deletions(-) create mode 100644 changes-entries/pr69767.txt diff --git a/changes-entries/pr69767.txt b/changes-entries/pr69767.txt new file mode 100644 index 00000000000..de02414769e --- /dev/null +++ b/changes-entries/pr69767.txt @@ -0,0 +1,3 @@ + *) mod_unixd: Drop test that effective user ID is zero in + a chroot configuration. PR 69767. + [Bastien Roucaries ] diff --git a/modules/arch/unix/mod_unixd.c b/modules/arch/unix/mod_unixd.c index 4ce279856f9..7a996aabe21 100644 --- a/modules/arch/unix/mod_unixd.c +++ b/modules/arch/unix/mod_unixd.c @@ -152,12 +152,6 @@ AP_DECLARE(int) ap_unixd_setup_child(void) } if (NULL != ap_unixd_config.chroot_dir) { - if (geteuid()) { - ap_log_error(APLOG_MARK, APLOG_ALERT, 0, NULL, APLOGNO(02158) - "Cannot chroot when not started as root"); - return EPERM; - } - if (chdir(ap_unixd_config.chroot_dir) != 0) { rv = errno; ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL, APLOGNO(02159)