]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: startup: revisit the default chroot behavior
authorWilly Tarreau <w@1wt.eu>
Tue, 24 Nov 2026 13:58:57 +0000 (14:58 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 24 Nov 2026 14:24:56 +0000 (15:24 +0100)
A few users reported the following pattern at boot:

  geteuid()                               = 1000
  prctl(PR_CAPBSET_READ, CAP_SYS_CHROOT)  = 1
  mkdir("/tmp/haproxy.wbFFQW", 0700)      = 0
  chdir("/tmp/haproxy.wbFFQW")            = 0
  rmdir("/tmp/haproxy.wbFFQW")            = 0
  chroot(".")                             = -1 EPERM (Operation not permitted)
  geteuid()                               = 1000
  getegid()                               = 1000
  unshare(CLONE_NEWUSER)                  = 0
  chroot(".")                             = -1 EPERM (Operation not permitted)

I.e. everything looks like we'llb be able to chroot but we fail to. And
that's pretty annoying because it reports a warning after that, speaking
about starting as root while it is not the case.

A first approach would consist in relaxing the rules around "auto" but
that's not good because users writing "chroot auto" *want* a chroot to
be performed (and it's actually the safest one since it has no "." nor
"..").

This patch clarifies the situation by introducing a new "opportunistic"
mode, that noone needs to type since it's the default. In this mode, no
error is reported during the attempt to chroot. However on failure, if
the user was root, a warning is reported to indicate that it's strange,
and suggests to use chroot / instead if needed.

In this case the conditions producing the trace above will no longer
display any irritating message on a config without chroot directive,
but users of "chroot auto" will still be told what's wrong.

This only affects 3.5 so no backport is needed.

doc/configuration.txt
src/haproxy.c

index 3f04d348366b0d5d61a57688215fbf2bee2a9fbf..fd05d67496d07f998c0659ff35d532d152d4b4d0 100644 (file)
@@ -2364,7 +2364,7 @@ ca-base <dir>
   directives. Absolute locations specified in "ca-file", "ca-verify-file" and
   "crl-file" prevail and ignore "ca-base".
 
-chroot { <jail dir> | auto }
+chroot { <jail dir> | auto | opportunistic }
   Changes current directory to <jail dir> and performs a chroot() there before
   dropping privileges. This increases the security level in case an unknown
   vulnerability would be exploited, since it would make it very hard for the
@@ -2378,15 +2378,17 @@ chroot { <jail dir> | auto }
   As a special case, <jail dir> may be set to "auto", in which case haproxy
   creates an anonymous temporary directory, unlinks it, and chroots into it.
   The resulting jail has no name in the filesystem and is empty and read-only,
-  removing the need to prepare a dedicated jail directory.
+  removing the need to prepare a dedicated jail directory. "opportunistic" is
+  the same except that no error is displayed on failure.
 
   When starting with sufficient privileges and when "set-dumpable" is not set,
-  haproxy will automatically apply "chroot auto" if no "chroot" directive is
-  used, i.e. it will isolate itself in a chroot jail, and will emit a warning
-  if it fails to do so. If there is a compelling reason not to use chroot (e.g.
-  access to a server via a UNIX socket outside of the jail), it remains
-  possible to silence the warning by adding an explicit "chroot /", which has
-  the benefit of being visible in a configuration.
+  haproxy will automatically apply "chroot opportunistic" if no "chroot"
+  directive is used, i.e. it will try to isolate itself in a chroot jail, and
+  will only emit a warning if it fails to do so while being started as root. If
+  there is a compelling reason not to use chroot (e.g. access to a server via a
+  UNIX socket outside of the jail), it remains possible to silence the warning
+  by adding an explicit "chroot /", which has the benefit of being visible in a
+  configuration.
 
 close-spread-time <time>
   Define a time window during which idle connections and active connections
index 59fd2a9060c70fba866ad59ec865923fc8521f00..00adee27ce0ca30817904ee3238a82886de3036a 100644 (file)
@@ -3420,22 +3420,37 @@ static void setup_user_ns(uid_t euid, gid_t egid)
 }
 #endif
 
+/* Try to chroot in directory <path>. Returns <0 on failure, 0 on success. If
+ * <path> is "auto", a temporary directory is created on the fly under /tmp to
+ * chroot into. If <path> is "opportunistic", then it is handled like "auto"
+ * except that everything is made as a best-effort attempt and no alert is
+ * shown on failure (but the caller still detects it). On systems supporting
+ * them, a user namespace may be created before retrying when facing EPERM,
+ * which would permits non-privileged users to chroot as well.
+ */
 static int do_chroot(const char *prog, const char *path)
 {
        const char *dir, *chroot_dir;
        int error, chroot_error;
-       char tmpdir[] = "/tmp/haproxy.XXXXXX";
+       int is_auto = 0;
+       int is_opp = 0;
 
+       is_auto = strcmp(path, "auto") == 0;
+       is_opp = strcmp(path, "opportunistic") == 0;
        error = chroot_error = 0;
        dir = chroot_dir = path;
-       if (strcmp(path, "auto") == 0) {
-               /* When "chroot auto" is used, we attempt to chroot to an
-                * anonymous and read-only directory.
+
+       if (is_auto || is_opp) {
+               /* When "chroot auto" or "chroot opportunistic" is used, we
+                * attempt to chroot to an anonymous and read-only directory.
                 */
+               char tmpdir[] = "/tmp/haproxy.XXXXXX";
+
                dir = mkdtemp(tmpdir);
                if (dir == NULL) {
-                       ha_alert("[%s.main()] Cannot create(%s) for chroot auto.\n",
-                                prog, tmpdir);
+                       if (!is_opp)
+                               ha_alert("[%s.main()] Cannot create(%s) for chroot auto.\n",
+                                        prog, tmpdir);
                        return -1;
                }
                error = chdir(tmpdir);
@@ -3469,7 +3484,8 @@ static int do_chroot(const char *prog, const char *path)
                error = chdir("/");
 
        if (error || chroot_error) {
-               ha_alert("[%s.main()] Cannot chroot(%s).\n", prog, dir);
+               if (!is_opp)
+                       ha_alert("[%s.main()] Cannot chroot(%s).\n", prog, dir);
                return -1;
        }
 
@@ -3803,34 +3819,26 @@ int main(int argc, char **argv)
                }
        }
 
-       /* privileged users should use chroot whenever possible; use chroot /
-        * if really not wanted.
+       /* privileged users should use chroot whenever possible, and anyone
+        * with enough permission to try to do so; use chroot / if really not
+        * wanted. Let's try below, and silently fail. For root however, a
+        * warning is emitted because that's not expected.
         */
-
        if (!global.chroot && !(global.tune.options & GTUNE_SET_DUMPABLE)) {
                int chroot_permitted = geteuid() == 0;
 
-#if defined(USE_PRCTL) && defined(PR_CAPBSET_READ) && defined(CAP_SYS_CHROOT)
-               int ret = prctl(PR_CAPBSET_READ, CAP_SYS_CHROOT, 0, 0, 0);
-               if (ret == 1)
-                       chroot_permitted = 1;
-               else if (ret == 0)
-                       chroot_permitted = 0;
-               /* errors mean prctl() not supported, so don't update the verdict */
-#endif
-               if (chroot_permitted && do_chroot(argv[0], "auto") != 0) {
-                       ha_warning("[%s.main()] HAProxy was started as root without any 'chroot' "
-                                  "directive, and an attempt at automatically chrooting failed, "
-                                  "so the haproxy process is not as protected as it could be. "
-                                  "Please report this event to developers with as many details as "
-                                  "possible about your execution environment so they can evaluate "
-                                  "if anything can be done to better support it. A chroot limits "
-                                  "filesystem access of an intruder to a single, preferably empty, "
-                                  "directory. It is strongly recommended to enable this feature "
-                                  "whenever possible (it's always possible when starting as root), "
-                                  "via 'chroot auto' in the global section. If you think you have "
-                                  "good reasons for running outside a chroot, explicitly configure "
-                                  "'chroot /' to silence this warning.\n", argv[0]);
+               if (do_chroot(argv[0], "opportunistic") != 0) {
+                       if (chroot_permitted)
+                               ha_warning("[%s.main()] "
+                                          "HAProxy was started as root without any 'chroot' directive"
+                                          "directive, and an attempt at opportunistically chrooting failed, "
+                                          "so the haproxy process is not as protected as it could be. "
+                                          "A chroot limits filesystem access of an intruder to a single, "
+                                          "preferably empty, directory. It is strongly recommended to enable "
+                                          "this feature whenever possible (it should always be possible when "
+                                          "starting as root), via 'chroot auto' in the global section. If you "
+                                          "think you have good reasons for running outside a chroot, explicitly "
+                                          "configure 'chroot /' to silence this warning.\n", argv[0]);
                }
        }